diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 000000000..0de281cac --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,47 @@ +name: PHPStan + +on: + push: + branches: + - 'master' + - 'release/**' + paths-ignore: + - '**.md' + - '**.txt' + pull_request: + branches: + - 'master' + +jobs: + phpstan: + name: 'WP latest on PHP 8.3' + + runs-on: ubuntu-20.04 + + steps: + - uses: actions/checkout@v4 + + # TODO Use the composer action below + - uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + extensions: uopz + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies (other PHP versions) + run: | + composer install --prefer-dist --no-progress --ignore-platform-reqs + + - name: Run static suite + run: | + composer dump-autoload -o + + #- uses: actions/checkout@v4 + #- uses: php-actions/composer@v6 + + - name: PHPStan Static Analysis + uses: php-actions/phpstan@v3 + with: + configuration: phpstan.neon diff --git a/composer.json b/composer.json index 1de07f415..7232b3860 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "platform-check": false, "allow-plugins": { "php-http/discovery": true, - "dealerdirect/phpcodesniffer-composer-installer": true + "dealerdirect/phpcodesniffer-composer-installer": true, + "phpstan/extension-installer": true } }, "autoload": { @@ -55,6 +56,8 @@ "slope-it/clock-mock": "^0.4.0", "phpunit/php-code-coverage": "^9.2.32", "myclabs/deep-copy": "^1.12.0", - "phpstan/phpstan": "^1.12" + "phpstan/phpstan": "^1.12", + "szepeviktor/phpstan-wordpress": "^1.3", + "phpstan/extension-installer": "^1.4" } } diff --git a/composer.lock b/composer.lock index a02873157..d13a99a74 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a1e4eab161c1593cc128f998fbbd9d15", + "content-hash": "5b9fe8b5e3036d759f14932c21c98e50", "packages": [ { "name": "clue/stream-filter", @@ -2708,6 +2708,54 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "php-stubs/wordpress-stubs", + "version": "v6.7.1", + "source": { + "type": "git", + "url": "https://github.com/php-stubs/wordpress-stubs.git", + "reference": "83448e918bf06d1ed3d67ceb6a985fc266a02fd1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-stubs/wordpress-stubs/zipball/83448e918bf06d1ed3d67ceb6a985fc266a02fd1", + "reference": "83448e918bf06d1ed3d67ceb6a985fc266a02fd1", + "shasum": "" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", + "nikic/php-parser": "^4.13", + "php": "^7.4 || ^8.0", + "php-stubs/generator": "^0.8.3", + "phpdocumentor/reflection-docblock": "^5.4.1", + "phpstan/phpstan": "^1.11", + "phpunit/phpunit": "^9.5", + "szepeviktor/phpcs-psr-12-neutron-hybrid-ruleset": "^1.1.1", + "wp-coding-standards/wpcs": "3.1.0 as 2.3.0" + }, + "suggest": { + "paragonie/sodium_compat": "Pure PHP implementation of libsodium", + "symfony/polyfill-php80": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions", + "szepeviktor/phpstan-wordpress": "WordPress extensions for PHPStan" + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "WordPress function and class declaration stubs for static analysis.", + "homepage": "https://github.com/php-stubs/wordpress-stubs", + "keywords": [ + "PHPStan", + "static analysis", + "wordpress" + ], + "support": { + "issues": "https://github.com/php-stubs/wordpress-stubs/issues", + "source": "https://github.com/php-stubs/wordpress-stubs/tree/v6.7.1" + }, + "time": "2024-11-24T03:57:09+00:00" + }, { "name": "phpcompatibility/php-compatibility", "version": "9.3.5", @@ -3078,6 +3126,54 @@ ], "time": "2024-05-20T13:34:27+00:00" }, + { + "name": "phpstan/extension-installer", + "version": "1.4.3", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "85e90b3942d06b2326fba0403ec24fe912372936" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/85e90b3942d06b2326fba0403ec24fe912372936", + "reference": "85e90b3942d06b2326fba0403ec24fe912372936", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0 || ^2.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.4.3" + }, + "time": "2024-09-04T20:21:43+00:00" + }, { "name": "phpstan/phpstan", "version": "1.12.12", @@ -4655,6 +4751,69 @@ ], "time": "2024-09-18T10:38:58+00:00" }, + { + "name": "szepeviktor/phpstan-wordpress", + "version": "v1.3.5", + "source": { + "type": "git", + "url": "https://github.com/szepeviktor/phpstan-wordpress.git", + "reference": "7f8cfe992faa96b6a33bbd75c7bace98864161e7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/szepeviktor/phpstan-wordpress/zipball/7f8cfe992faa96b6a33bbd75c7bace98864161e7", + "reference": "7f8cfe992faa96b6a33bbd75c7bace98864161e7", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "php-stubs/wordpress-stubs": "^4.7 || ^5.0 || ^6.0", + "phpstan/phpstan": "^1.10.31", + "symfony/polyfill-php73": "^1.12.0" + }, + "require-dev": { + "composer/composer": "^2.1.14", + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", + "php-parallel-lint/php-parallel-lint": "^1.1", + "phpstan/phpstan-strict-rules": "^1.2", + "phpunit/phpunit": "^8.0 || ^9.0", + "szepeviktor/phpcs-psr-12-neutron-hybrid-ruleset": "^1.0", + "wp-coding-standards/wpcs": "3.1.0 as 2.3.0" + }, + "suggest": { + "swissspidy/phpstan-no-private": "Detect usage of internal core functions, classes and methods" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "extension.neon" + ] + } + }, + "autoload": { + "psr-4": { + "SzepeViktor\\PHPStan\\WordPress\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "WordPress extensions for PHPStan", + "keywords": [ + "PHPStan", + "code analyse", + "code analysis", + "static analysis", + "wordpress" + ], + "support": { + "issues": "https://github.com/szepeviktor/phpstan-wordpress/issues", + "source": "https://github.com/szepeviktor/phpstan-wordpress/tree/v1.3.5" + }, + "time": "2024-06-28T22:27:19+00:00" + }, { "name": "theseer/tokenizer", "version": "1.2.3", diff --git a/includes/Admin.php b/includes/Admin.php index b030bfb86..6d2974095 100644 --- a/includes/Admin.php +++ b/includes/Admin.php @@ -287,7 +287,7 @@ function commonsbooking_sanitizeArrayorString( $data, $sanitizeFunction = 'sanit * @param mixed $log can be a string, array or object * @param bool $backtrace if set true the file-path and line of the calling file will be added to the error message * - * @return string logmessage + * @return void */ function commonsbooking_write_log( $log, $backtrace = true ) { @@ -308,6 +308,5 @@ function commonsbooking_write_log( $log, $backtrace = true ) { $logmessage = $file . ':' . $line . ' ' . $logmessage; } - error_log( $logmessage ) ; - + error_log( $logmessage ); } \ No newline at end of file diff --git a/includes/Template.php b/includes/Template.php index 7b7da8a38..d51fa5476 100644 --- a/includes/Template.php +++ b/includes/Template.php @@ -81,5 +81,6 @@ function commonsbooking_get_template_part( $slug, $name = '', $include = true, $ } else if ( $template && $include === false ) { return $before_html . $template . $after_html; } + return ''; } } diff --git a/includes/TemplateParser.php b/includes/TemplateParser.php index bcebd5049..9e87a7374 100644 --- a/includes/TemplateParser.php +++ b/includes/TemplateParser.php @@ -100,9 +100,9 @@ function commonsbooking_parse_template_callback( $match, array $objects = [], $s /** * Return Custom Post Type postType for template type string * - * @param [type] $type type could be location, booking, item + * @param string $type type could be location, booking, item * - * @return void + * @return string */ function commonsbooking_getCBType( $type ) { if ( $type == 'location' ) { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 000000000..5cbf5b993 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - + message: "#^Action callback returns bool but should not return anything\\.$#" + count: 1 + path: src/Wordpress/CustomPostType/CustomPostType.php diff --git a/phpstan.neon b/phpstan.neon index 483172c90..c5717c45c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,18 +1,19 @@ +includes: + - phpstan-baseline.neon parameters: phpVersion: 70400 scanFiles: - commonsbooking.php - vendor/cmb2/cmb2/includes/helper-functions.php - scanDirectories: - - /tmp/wordpress - - /tmp/wordpress-tests-lib + - vendor/cmb2/cmb2/includes/CMB2_Utils.php + - vendor/cmb2/cmb2/includes/CMB2_Field.php paths: - commonsbooking.php - src/ - - tests/ - includes/ level: 1 ignoreErrors: - - identifier: missingType.iterableValue - '#Constant (COMMONSBOOKING.*|WP_DEBUG_LOG) not found.#' + - '#Instantiated class (CommonsBooking.*CB_Data) not found.#' + - '#Function cb_object_to_array not found.#' diff --git a/src/CB/CB1UserFields.php b/src/CB/CB1UserFields.php index a83eaa792..68d56cad5 100644 --- a/src/CB/CB1UserFields.php +++ b/src/CB/CB1UserFields.php @@ -227,6 +227,7 @@ public function registration_add_meta( $user_id ) { /** * Sets a flat array of user field/value pairs * + * @since 2.10 deprecated (cb_object_to_array is unspecified) * @since 0.6 * */ diff --git a/src/Service/Cache.php b/src/Service/Cache.php index 0c492fa74..bc6c151b6 100644 --- a/src/Service/Cache.php +++ b/src/Service/Cache.php @@ -208,14 +208,15 @@ public static function clearCache( array $tags = [] ) { * Calls clearCache using WP Cron. * Why? ClearCache can be resource intensive on larger instances and should be offloaded. * - * @param array $tags + * @param array $tags to clear cache for * * @return void */ public static function scheduleClearCache( array $tags = [] ) { - $event = wp_schedule_single_event( time(), self::$clearCacheHook, [ $tags ] ); + $event = wp_schedule_single_event( time(), self::$clearCacheHook, [ $tags ], true ); + // TODO document why only on wp-error, why this can fail, why we don't re-try or do other things, instead of forcing the execution of this resource intensive task? if ( is_wp_error( $event ) ) { - //run the event right away when scheduling fails + // runs the event right away, when scheduling fails self::clearCache( $tags ); } } diff --git a/src/Service/Holiday.php b/src/Service/Holiday.php index 3358979e0..58e1220fb 100644 --- a/src/Service/Holiday.php +++ b/src/Service/Holiday.php @@ -13,7 +13,7 @@ class Holiday { * @param $object_type * @param $field_type * - * @return void + * @return string */ public static function renderFields( $field, $value, $object_id, $object_type, $field_type ) { @@ -61,6 +61,8 @@ public static function renderFields( $field, $value, $object_id, $object_type, $
_desc( true ); + + return ''; } diff --git a/src/Wordpress/CustomPostType/CustomPostType.php b/src/Wordpress/CustomPostType/CustomPostType.php index c2aa5955f..c68aceddc 100644 --- a/src/Wordpress/CustomPostType/CustomPostType.php +++ b/src/Wordpress/CustomPostType/CustomPostType.php @@ -138,7 +138,7 @@ public static function getCMB2FieldsArrayFromCustomMetadata( $type ): ?array { * * @param mixed $actions * - * @return void + * @return mixed */ public static function modifyRowActions( $actions, $post ) { diff --git a/src/Wordpress/CustomPostType/Map.php b/src/Wordpress/CustomPostType/Map.php index 3233c886a..c0b60cc3a 100644 --- a/src/Wordpress/CustomPostType/Map.php +++ b/src/Wordpress/CustomPostType/Map.php @@ -40,7 +40,7 @@ public function initHooks() { add_shortcode( 'cb_map', array( MapShortcode::class, 'execute' ) ); // Add actions - add_action( 'save_post_' . self::$postType, array( MapAdmin::class, 'validate_options' ), 10, 3 ); + add_action( 'save_post_' . self::$postType, array( MapAdmin::class, 'validate_options' ), 10, 1 ); add_action( 'add_meta_boxes_cb_map', array( MapAdmin::class, 'add_meta_boxes' ) ); } diff --git a/src/Wordpress/CustomPostType/Timeframe.php b/src/Wordpress/CustomPostType/Timeframe.php index 0282cbded..caaae53f3 100644 --- a/src/Wordpress/CustomPostType/Timeframe.php +++ b/src/Wordpress/CustomPostType/Timeframe.php @@ -1358,6 +1358,8 @@ public function setCustomColumnSortOrder( \WP_Query $query ) { /** * Initiates needed hooks. + * + * @return void */ public function initHooks() { // Add custom cmb2 type for email booking codes by cron