Skip to content

Commit

Permalink
Add PHPStan on tests (#6733)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabrisrp authored Jun 21, 2024
1 parent 5b286ce commit a8da7ae
Show file tree
Hide file tree
Showing 39 changed files with 249 additions and 398 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
"mikey179/vfsstream": "1.6.11",
"mnsami/composer-custom-directory-installer": "^2.0",
"mobiledetect/mobiledetectlib": "^2.8",
"php-stubs/wordpress-tests-stubs": "^6.5",
"phpcompatibility/phpcompatibility-wp": "^2.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-phpunit": "^1.4",
"phpunit/phpunit": "^7.5 || ^8 || ^9",
"psr/container": "1.1.1",
"roave/security-advisories": "dev-master",
Expand All @@ -74,6 +75,7 @@
"wpackagist-plugin/pdf-embedder": "4.6.*",
"wpackagist-plugin/simple-custom-css": "^4.0.3",
"wpackagist-plugin/spinupwp": "^1.1",
"wpackagist-plugin/the-events-calendar": "6.5.0.1",
"wpackagist-plugin/woocommerce": "^8",
"wpackagist-plugin/wp-smushit": "^3"
},
Expand Down
1 change: 0 additions & 1 deletion inc/ThirdParty/Hostings/Godaddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Godaddy implements Subscriber_Interface {
* @param string $vip_url Godaddy vip url.
*/
public function __construct( $vip_url = '' ) {
// @phpstan-ignore-next-line
$this->vip_url = method_exists( '\WPaas\Plugin', 'vip' ) ? \WPaas\Plugin::vip() : $vip_url;
}

Expand Down
2 changes: 0 additions & 2 deletions inc/ThirdParty/Hostings/Pressidium.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function clear_cache_after_pressidium() {
* @return void
*/
public function clean_pressidium() {
// @phpstan-ignore-next-line
$plugin = NinukisCaching::get_instance();
$plugin->purgeAllCaches();
}
Expand Down Expand Up @@ -107,7 +106,6 @@ private function get_paths( $urls ) {
* @return void
*/
private function purge_cache( $paths ) {
// @phpstan-ignore-next-line
NinukisCaching::get_instance()->purge_cache( $paths );
}

Expand Down
4 changes: 0 additions & 4 deletions inc/ThirdParty/Hostings/Savvii.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,14 @@ public function varnish_addon_title( $settings ) {
public function clear_cache_after_savvii() {
if (
! (
// @phpstan-ignore-next-line
isset( $_REQUEST[ CacheFlusherPlugin::NAME_FLUSH_NOW ] )
&&
// @phpstan-ignore-next-line
check_admin_referer( CacheFlusherPlugin::NAME_FLUSH_NOW )
)
&&
! (
// @phpstan-ignore-next-line
isset( $_REQUEST[ CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW ] )
&&
// @phpstan-ignore-next-line
check_admin_referer( CacheFlusherPlugin::NAME_DOMAINFLUSH_NOW )
)
) {
Expand Down
2 changes: 0 additions & 2 deletions inc/ThirdParty/Hostings/WPEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ public function varnish_addon_title( $settings ) {
*/
public function clean_wpengine() {
if ( method_exists( 'WpeCommon', 'purge_memcached' ) ) {
// @phpstan-ignore-next-line
WpeCommon::purge_memcached();
}

if ( method_exists( 'WpeCommon', 'purge_varnish_cache' ) ) {
// @phpstan-ignore-next-line
WpeCommon::purge_varnish_cache();
}
}
Expand Down
6 changes: 1 addition & 5 deletions inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,6 @@ public function exclude_product_shipping_taxonomy( $excluded_taxonomies ) {
* @return bool
*/
private function product_has_gallery_images() {
// @phpstan-ignore-next-line
$product = wc_get_product( get_the_ID() );
if ( empty( $product ) ) {
return false;
Expand All @@ -530,7 +529,6 @@ public function show_empty_product_gallery_with_delayJS() {
return;
}

// @phpstan-ignore-next-line
if ( ! is_product() ) {
return;
}
Expand Down Expand Up @@ -562,7 +560,6 @@ public function show_notempty_product_gallery_with_delayJS( $exclusions = [] ):
return $exclusions;
}

// @phpstan-ignore-next-line
if ( ! is_product() ) {
return $exclusions;
}
Expand Down Expand Up @@ -616,14 +613,13 @@ public function disallow_rocket_clean_post(): void {
*/
public function allow_rocket_clean_post( int $product_id ): void {
$urls = [];
// @phpstan-ignore-next-line

$category_list = wc_get_product_category_list( $product_id );

if ( preg_match_all( '/<a\s+(?:[^>]*?\s+)?href=(["\'])(?<urls>.*?)\1/i', $category_list, $matches ) ) {
$urls = $matches['urls'];
}

// @phpstan-ignore-next-line
$shop_page = get_permalink( wc_get_page_id( 'shop' ) );

if ( empty( $shop_page ) ) {
Expand Down
9 changes: 1 addition & 8 deletions inc/ThirdParty/Plugins/I18n/TranslatePress.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function get_subscribed_events() {
* @return string
*/
public function detect_homepage( $home_url, $url ) {
// @phpstan-ignore-next-line

$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );

Expand Down Expand Up @@ -78,7 +78,6 @@ function_exists( 'trp_get_languages' )
* @return array
*/
public function add_langs_to_admin_bar( $langlinks ) {
// @phpstan-ignore-next-line
$translatepress = TRP_Translate_Press::get_trp_instance();

$language_switcher = $translatepress->get_component( 'language_switcher' );
Expand Down Expand Up @@ -129,7 +128,6 @@ public function get_active_languages_uri( $urls ) {

$home_url = home_url();

// @phpstan-ignore-next-line
$translatepress = TRP_Translate_Press::get_trp_instance();

$settings = $translatepress->get_component( 'settings' );
Expand Down Expand Up @@ -159,7 +157,6 @@ public function get_active_languages_codes( $codes ) {
$codes = (array) $codes;
}

// @phpstan-ignore-next-line
$translatepress = TRP_Translate_Press::get_trp_instance();

$settings = $translatepress->get_component( 'settings' );
Expand Down Expand Up @@ -189,7 +186,6 @@ public function get_home_url_for_lang( $home_url, $lang ) {
return $home_url;
}

// @phpstan-ignore-next-line
$translatepress = TRP_Translate_Press::get_trp_instance();
$converter = $translatepress->get_component( 'url_converter' );
$settings = $translatepress->get_component( 'settings' );
Expand Down Expand Up @@ -228,7 +224,6 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
$urls = (array) $urls;
}

// @phpstan-ignore-next-line
$translatepress = TRP_Translate_Press::get_trp_instance();

$settings = $translatepress->get_component( 'settings' );
Expand All @@ -254,7 +249,6 @@ public function get_translated_post_urls( $urls, $url, $post_type, $regex ) {
* @return void
*/
public function clear_post_languages( $post_id ) {
// @phpstan-ignore-next-line
$translatepress = TRP_Translate_Press::get_trp_instance();

$converter = $translatepress->get_component( 'url_converter' );
Expand Down Expand Up @@ -293,7 +287,6 @@ public function clear_post_languages( $post_id ) {
* @return void
*/
public function clear_post_after_updating_translation( $update_strings, $settings ) {
// @phpstan-ignore-next-line
$translatepress = TRP_Translate_Press::get_trp_instance();

$converter = $translatepress->get_component( 'url_converter' );
Expand Down
1 change: 0 additions & 1 deletion inc/ThirdParty/Plugins/SEO/SEOPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static function get_subscribed_events() {
return [];
}

// @phpstan-ignore-next-line
if ( ! method_exists( seopress_get_service( 'SitemapOption' ), 'isEnabled' ) || 1 !== (int) seopress_get_service( 'SitemapOption' )->isEnabled() ) {
return [];
}
Expand Down
4 changes: 0 additions & 4 deletions inc/ThirdParty/Plugins/SEO/TheSEOFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public function add_tsf_sitemap_to_preload( $sitemaps ) {
// The autoloader in TSF doesn't check for file_exists(). So, use version compare instead to prevent fatal errors.
if ( version_compare( rocket_get_constant( 'THE_SEO_FRAMEWORK_VERSION', false ), '4.0', '>=' ) ) {
// TSF 4.0+. Expect the class to exist indefinitely.

// @phpstan-ignore-next-line
$sitemap_bridge = Sitemap::get_instance();

foreach ( $sitemap_bridge->get_sitemap_endpoint_list() as $id => $data ) {
Expand All @@ -80,8 +78,6 @@ public function add_tsf_sitemap_to_preload( $sitemaps ) {
}
} else {
// Deprecated. TSF <4.0.

// @phpstan-ignore-next-line
$sitemaps[] = the_seo_framework()->get_sitemap_xml_url();
}

Expand Down
7 changes: 0 additions & 7 deletions inc/ThirdParty/Plugins/Security/WordFenceCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public function pop_old_ip( string $old_rucss_ip ) {
}

// Update whitelist.

// @phpstan-ignore-next-line
wfConfig::set( 'whitelisted', implode( ',', $whitelist ) );
}

Expand All @@ -76,8 +74,6 @@ public function pop_ip_from_whitelist() {
}

// Update whitelist.

// @phpstan-ignore-next-line
wfConfig::set( 'whitelisted', implode( ',', $whitelist ) );
}

Expand All @@ -89,8 +85,6 @@ public function pop_ip_from_whitelist() {
*/
private function can_pop_ip( string $ip ) {
// Get all whitelists.

// @phpstan-ignore-next-line
$whitelists = wfConfig::get( 'whitelisted', '' );

// Convert to array.
Expand Down Expand Up @@ -134,7 +128,6 @@ public function whitelist_wordfence_firewall_ips() {
}

foreach ( $ips as $ip ) {
// @phpstan-ignore-next-line
wordfence::whitelistIP( $ip );
}
}
Expand Down
81 changes: 46 additions & 35 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- phar://phpstan.phar/conf/bleedingEdge.neon
parameters:
level: 0
checkMissingIterableValueType: false
inferPrivatePropertyTypeFromConstructor: true
paths:
# Test only the new architecture for now.
- %currentWorkingDirectory%/inc/Engine/
- %currentWorkingDirectory%/inc/Addon/
- %currentWorkingDirectory%/inc/ThirdParty/
bootstrapFiles:
# Must be first
- %currentWorkingDirectory%/inc/functions/options.php
# Procedural code
- %currentWorkingDirectory%/inc/constants.php
- %currentWorkingDirectory%/inc/common/purge.php
- %currentWorkingDirectory%/inc/API/bypass.php
- %currentWorkingDirectory%/inc/3rd-party/plugins/autoptimize.php
- %currentWorkingDirectory%/inc/admin/ui/notices.php
- %currentWorkingDirectory%/inc/functions/admin.php
- %currentWorkingDirectory%/inc/functions/api.php
- %currentWorkingDirectory%/inc/functions/files.php
- %currentWorkingDirectory%/inc/functions/formatting.php
- %currentWorkingDirectory%/inc/functions/htaccess.php
- %currentWorkingDirectory%/inc/functions/i18n.php
- %currentWorkingDirectory%/inc/functions/posts.php
# Composer autoloader "exclude-from-classmap"
- %currentWorkingDirectory%/inc/classes/class-wp-rocket-requirements-check.php
- %currentWorkingDirectory%/inc/vendors/classes/class-rocket-mobile-detect.php
scanDirectories:
- %currentWorkingDirectory%/inc/Dependencies/ActionScheduler
excludePaths:
- %currentWorkingDirectory%/inc/vendors/
# These need plugin stubs!
- %currentWorkingDirectory%/inc/classes/subscriber/third-party/
- %currentWorkingDirectory%/inc/3rd-party/
level: 0
inferPrivatePropertyTypeFromConstructor: true
paths:
# Test only the new architecture for now.
- %currentWorkingDirectory%/inc/Engine/
- %currentWorkingDirectory%/inc/Addon/
- %currentWorkingDirectory%/inc/ThirdParty/
- %currentWorkingDirectory%/tests/Integration/
- %currentWorkingDirectory%/tests/Unit/
bootstrapFiles:
# Must be first
- %currentWorkingDirectory%/inc/functions/options.php
# Procedural code
- %currentWorkingDirectory%/inc/constants.php
- %currentWorkingDirectory%/inc/common/purge.php
- %currentWorkingDirectory%/inc/API/bypass.php
- %currentWorkingDirectory%/inc/3rd-party/plugins/autoptimize.php
- %currentWorkingDirectory%/inc/admin/ui/notices.php
- %currentWorkingDirectory%/inc/functions/admin.php
- %currentWorkingDirectory%/inc/functions/api.php
- %currentWorkingDirectory%/inc/functions/files.php
- %currentWorkingDirectory%/inc/functions/formatting.php
- %currentWorkingDirectory%/inc/functions/htaccess.php
- %currentWorkingDirectory%/inc/functions/i18n.php
- %currentWorkingDirectory%/inc/functions/posts.php
# Composer autoloader "exclude-from-classmap"
- %currentWorkingDirectory%/inc/classes/class-wp-rocket-requirements-check.php
- %currentWorkingDirectory%/inc/vendors/classes/class-rocket-mobile-detect.php
scanDirectories:
- %currentWorkingDirectory%/inc/Dependencies/ActionScheduler
- %currentWorkingDirectory%/tests/Fixtures/
- %currentWorkingDirectory%/vendor/antecedent/patchwork/
- %currentWorkingDirectory%/vendor/wpackagist-plugin/amp/
- %currentWorkingDirectory%/vendor/wpackagist-plugin/the-events-calendar/
- %currentWorkingDirectory%/vendor/wpackagist-plugin/woocommerce/
scanFiles:
- %currentWorkingDirectory%/inc/admin/upgrader.php
- %currentWorkingDirectory%/inc/admin/options.php
- %currentWorkingDirectory%/vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
- %currentWorkingDirectory%/vendor/php-stubs/wordpress-tests-stubs/wordpress-tests-stubs.php
excludePaths:
- %currentWorkingDirectory%/inc/vendors/
# These need plugin stubs!
- %currentWorkingDirectory%/inc/classes/subscriber/third-party/
- %currentWorkingDirectory%/inc/3rd-party/
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

namespace WP_Rocket\Tests\Integration\inc\Engine\Optimization\Minify\CSS\AdminSubscriber;

use WP_Rocket\Tests\Integration\TestCase;
use Brain\Monkey\Functions;
use WP_Rocket\Tests\Integration\TestCase;

/**
* Test class covering \WP_Rocket\Engine\Optimization\Minify\CSS\AdminSubscriber::on_update
*
* @group AdminOnly
* @group MinifyAdmin
*/
class Test_OnUpdate extends TestCase {
class TestOnUpdate extends TestCase {
public function set_up() {
parent::set_up();

remove_filter( 'wp_rocket_upgrade', 'rocket_new_upgrade' );
}

public function tear_down() {
add_filter( 'wp_rocket_upgrade', 'rocket_new_upgrade' );
add_filter( 'wp_rocket_upgrade', 'rocket_new_upgrade', 10, 2 );

parent::tear_down();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace WP_Rocket\Tests\integration\inc\ThirdParty\Hostings\Godaddy;
namespace WP_Rocket\Tests\Integration\inc\ThirdParty\Hostings\Godaddy;

use WP_Rocket\Tests\Integration\TestCase;

Expand Down
Loading

0 comments on commit a8da7ae

Please sign in to comment.