Skip to content

Commit 69abd5e

Browse files
committed
phpstan adjust
1 parent 665c8ab commit 69abd5e

File tree

5 files changed

+76
-29
lines changed

5 files changed

+76
-29
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ parameters:
1515
- '#Call to static method l\(\) on an unknown class WP2Static\\WsLog.#'
1616
- '#Call to static method getUrl\(\) on an unknown class WP2Static\\SiteInfo.#'
1717
- '#Call to static method getPath\(\) on an unknown class WP2Static\\SiteInfo.#'
18+
- '#Call to static method fileisCached\(\) on an unknown class WP2Static\\DeployCache.#'
19+
- '#Call to static method addFile\(\) on an unknown class WP2Static\\DeployCache.#'

src/CLI.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace WP2StaticS3;
4+
5+
use WP_CLI;
6+
7+
8+
/**
9+
* WP2StaticS3 WP-CLI commands
10+
*
11+
* Registers WP-CLI commands for WP2StaticS3 under main wp2static cmd
12+
*
13+
* Usage: wp wp2static options set s3Bucket mybucketname
14+
*/
15+
class CLI {
16+
17+
/**
18+
* S3 commands
19+
*
20+
* @param string[] $args CLI args
21+
* @param string[] $assoc_args CLI args
22+
*/
23+
public function s3(
24+
array $args,
25+
array $assoc_args
26+
) : void {
27+
$action = isset( $args[0] ) ? $args[0] : null;
28+
29+
if ( empty( $action ) ) {
30+
WP_CLI::error( 'Missing required argument: <options>' );
31+
}
32+
33+
if ( $action === 'options' ) {
34+
WP_CLI::line( 'TBC setting options for S3 addon' );
35+
}
36+
}
37+
}
38+

src/Controller.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace WP2StaticS3;
44

55
class Controller {
6-
public function run() {
7-
// initialize options DB
6+
public function run() : void {
87
global $wpdb;
98

109
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
@@ -23,8 +22,6 @@ public function run() {
2322
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
2423
dbDelta( $sql );
2524

26-
// check for seed data
27-
// if deployment_url option doesn't exist, create:
2825
$options = $this->getOptions();
2926

3027
if ( ! isset( $options['s3Bucket'] ) ) {
@@ -49,21 +46,17 @@ public function run() {
4946

5047
add_action(
5148
'wp2static_post_deploy_trigger',
52-
[ 'WP2StaticS3\Deployer', 'cloudfront_invalidate' ],
49+
[ 'WP2StaticS3\Deployer', 'cloudfront_invalidate_all_items' ],
5350
15,
5451
1
5552
);
5653

57-
// if ( defined( 'WP_CLI' ) ) {
58-
// \WP_CLI::add_command(
59-
// 'wp2static s3',
60-
// [ 'WP2StaticS3\CLI', 's3' ]);
61-
// }
62-
}
63-
64-
// TODO: is this needed? confirm slashing of destination URLs...
65-
public function modifyWordPressSiteURL( $site_url ) {
66-
return rtrim( $site_url, '/' );
54+
if ( defined( 'WP_CLI' ) ) {
55+
\WP_CLI::add_command(
56+
'wp2static s3',
57+
[ 'WP2StaticS3\CLI', 's3' ]
58+
);
59+
}
6760
}
6861

6962
/**
@@ -210,8 +203,10 @@ public static function seedOptions() : void {
210203

211204
/**
212205
* Save options
206+
*
207+
* @param mixed $value option value to save
213208
*/
214-
public static function saveOption( $name, $value ) : void {
209+
public static function saveOption( string $name, $value ) : void {
215210
global $wpdb;
216211

217212
$table_name = $wpdb->prefix . 'wp2static_addon_s3_options';
@@ -238,7 +233,7 @@ public static function renderS3Page() : void {
238233
}
239234

240235

241-
public function deploy( $processed_site_path ) {
236+
public function deploy( string $processed_site_path ) : void {
242237
\WP2Static\WsLog::l( 'S3 Addon deploying' );
243238

244239
$s3_deployer = new Deployer();
@@ -249,7 +244,7 @@ public function deploy( $processed_site_path ) {
249244
* Naive encypting/decrypting
250245
*
251246
*/
252-
public static function encrypt_decrypt( $action, $string ) {
247+
public static function encrypt_decrypt( string $action, string $string ) : string {
253248
$output = false;
254249
$encrypt_method = 'AES-256-CBC';
255250

@@ -268,13 +263,13 @@ public static function encrypt_decrypt( $action, $string ) {
268263

269264
if ( $action == 'encrypt' ) {
270265
$output = openssl_encrypt( $string, $encrypt_method, $key, 0, $variate );
271-
$output = base64_encode( $output );
266+
$output = base64_encode( (string) $output );
272267
} elseif ( $action == 'decrypt' ) {
273268
$output =
274269
openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $variate );
275270
}
276271

277-
return $output;
272+
return (string) $output;
278273
}
279274

280275
public static function activate_for_single_site() : void {
@@ -337,13 +332,19 @@ public static function activate( bool $network_wide = null ) : void {
337332
}
338333
}
339334

340-
public static function addSubmenuPage( $submenu_pages ) {
335+
/**
336+
* Add WP2Static submenu
337+
*
338+
* @param mixed[] $submenu_pages array of submenu pages
339+
* @return mixed[] array of submenu pages
340+
*/
341+
public static function addSubmenuPage( array $submenu_pages ) : array {
341342
$submenu_pages['s3'] = [ 'WP2StaticS3\Controller', 'renderS3Page' ];
342343

343344
return $submenu_pages;
344345
}
345346

346-
public static function saveOptionsFromUI() {
347+
public static function saveOptionsFromUI() : void {
347348
check_admin_referer( 'wp2static-s3-options' );
348349

349350
global $wpdb;

src/Deployer.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Aws\S3\S3Client;
88
use Aws\CloudFront\CloudFrontClient;
99
use Aws\Exception\AwsException;
10+
use Aws\Credentials\Credentials;
1011

1112
// TODO: pull out of old core GuessMimeType( $local_file )
1213

@@ -19,7 +20,7 @@ class Deployer {
1920

2021
public function __construct() {}
2122

22-
public function upload_files( $processed_site_path ) : void {
23+
public function upload_files( string $processed_site_path ) : void {
2324
// check if dir exists
2425
if ( ! is_dir( $processed_site_path ) ) {
2526
return;
@@ -81,7 +82,7 @@ public function upload_files( $processed_site_path ) : void {
8182

8283
if ( ! $real_filepath ) {
8384
$err = 'Trying to add unknown file to Zip: ' . $filename;
84-
WsLog::l( $err );
85+
\WP2Static\WsLog::l( $err );
8586
continue;
8687
}
8788

@@ -99,6 +100,11 @@ public function upload_files( $processed_site_path ) : void {
99100
ltrim( str_replace( $processed_site_path, '', $filename ), '/' );
100101

101102
$finfo = finfo_open( FILEINFO_MIME_TYPE );
103+
104+
if ( ! $finfo ) {
105+
continue;
106+
}
107+
102108
$mime_type = finfo_file( $finfo, $filename );
103109

104110
$result = $s3->putObject(
@@ -119,12 +125,12 @@ public function upload_files( $processed_site_path ) : void {
119125
}
120126

121127

122-
public function cloudfront_invalidate_all_items() {
128+
public function cloudfront_invalidate_all_items() : void {
123129
if ( ! Controller::getValue( 'cfDistributionID' ) ) {
124130
return;
125131
}
126132

127-
\WsLog::l( 'Invalidating all CloudFront items' );
133+
\WP2Static\WsLog::l( 'Invalidating all CloudFront items' );
128134

129135
$client_options = [
130136
'profile' => 'default',
@@ -145,7 +151,7 @@ public function cloudfront_invalidate_all_items() {
145151
Controller::getValue( 's3SecretAccessKey' )
146152
) {
147153

148-
$credentials = new Aws\Credentials\Credentials(
154+
$credentials = new \Aws\Credentials\Credentials(
149155
Controller::getValue( 's3AccessKeyID' ),
150156
\WP2StaticS3\Controller::encrypt_decrypt(
151157
'decrypt',
@@ -156,7 +162,7 @@ public function cloudfront_invalidate_all_items() {
156162
$client_options['credentials'] = $credentials;
157163
}
158164

159-
$client = new Aws\CloudFront\CloudFrontClient( $client );
165+
$client = new \Aws\CloudFront\CloudFrontClient( $client_options );
160166

161167
try {
162168
$result = $client->createInvalidation(

tests/phpstan/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
define( 'WP_PLUGIN_DIR', '' );
77

88
// WP2Static constants
9-
define( 'WP2STATIC_ZIP_VERSION', '' );
9+
define( 'WP2STATIC_S3_VERSION', '' );

0 commit comments

Comments
 (0)