From 383ed8ca03b93f2752bb8dd60f3c3e7e4fda1039 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 20:55:49 +0000 Subject: [PATCH 1/2] Run PHPStan on the tests folder and fix reported errors Add `tests` to the PHPStan paths (excluding the `tests/data` extraction fixtures and the `tests/phpstan/scan-files.php` stub file) and scan `WP_CLI\Tests\TestCase` from wp-cli-tests, which is not autoloaded. Fixes for the 120 errors reported in tests/: - Add `: void` return types to test methods and set_up()/tear_down(). - Add @param/@return docs (incl. data provider array shapes) to helpers. - Use `self::$base` instead of `static::$base` for a private property. - Replace deprecated `Utils\normalize_path()` with `Path::normalize()`. - Handle `glob()` returning false in TranslationsTest::tear_down(). - Give TestIterableCodeExtractor a no-op `fromString()`, which the IterableCodeExtractor trait relies on (and whitelist its gettext-style method name in phpcs, like the src extractors). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5n3ZupvgHr4fEBJCGp --- phpcs.xml.dist | 1 + phpstan.neon.dist | 5 + tests/BladeGettextExtractorTest.php | 20 +-- tests/FileDataExtractorTest.php | 10 +- tests/IterableCodeExtractorTest.php | 148 +++++++++++-------- tests/JsFunctionsScannerTest.php | 12 +- tests/MakeJsonMapTest.php | 22 +-- tests/PhpFunctionsScannerTest.php | 8 +- tests/PotGeneratorTest.php | 2 +- tests/TranslationsTest.php | 23 +-- tests/includes/TestIterableCodeExtractor.php | 12 ++ 11 files changed, 156 insertions(+), 107 deletions(-) diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 89438443..4c302c33 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -59,6 +59,7 @@ */src/*(Extractor|Generator|Scanner)\.php$ */src/ParsedComment\.php$ + */tests/includes/TestIterableCodeExtractor\.php$ diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 6d6c2689..1e562a6b 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,11 +3,16 @@ parameters: paths: - src - i18n-command.php + - tests + excludePaths: + - tests/data + - tests/phpstan/scan-files.php scanDirectories: - vendor/wp-cli/wp-cli/php scanFiles: - vendor/php-stubs/wordpress-stubs/wordpress-stubs.php - tests/phpstan/scan-files.php + - vendor/wp-cli/wp-cli-tests/tests/includes/TestCase.php treatPhpDocTypesAsCertain: false diff --git a/tests/BladeGettextExtractorTest.php b/tests/BladeGettextExtractorTest.php index e39b896b..2fc88a3b 100644 --- a/tests/BladeGettextExtractorTest.php +++ b/tests/BladeGettextExtractorTest.php @@ -29,7 +29,7 @@ private function extract( $blade, $domain = 'foo-theme' ) { return $translations; } - public function test_extracts_bound_prop_with_translation_function() { + public function test_extracts_bound_prop_with_translation_function(): void { $translations = $this->extract( '' ); @@ -37,7 +37,7 @@ public function test_extracts_bound_prop_with_translation_function() { $this->assertNotNull( $translations->find( null, 'Hello' ) ); } - public function test_extracts_multiple_bound_props() { + public function test_extracts_multiple_bound_props(): void { $translations = $this->extract( '' ); @@ -46,7 +46,7 @@ public function test_extracts_multiple_bound_props() { $this->assertNotNull( $translations->find( null, 'Try again' ) ); } - public function test_extracts_bound_props_from_multiline_component_tag() { + public function test_extracts_bound_props_from_multiline_component_tag(): void { $blade = <<<'BLADE' assertNotNull( $translations->find( null, 'Please try again' ) ); } - public function test_extracts_bound_props_from_open_component_tag() { + public function test_extracts_bound_props_from_open_component_tag(): void { $blade = <<<'BLADE' {!! __('Content inside', 'foo-theme') !!} @@ -73,7 +73,7 @@ public function test_extracts_bound_props_from_open_component_tag() { $this->assertNotNull( $translations->find( null, 'Content inside' ) ); } - public function test_ignores_static_props() { + public function test_ignores_static_props(): void { $translations = $this->extract( '' ); @@ -82,7 +82,7 @@ public function test_ignores_static_props() { $this->assertNull( $translations->find( null, 'warning' ) ); } - public function test_does_not_match_non_component_html() { + public function test_does_not_match_non_component_html(): void { $translations = $this->extract( '{{ __(\'Link text\', \'foo-theme\') }}' ); @@ -92,7 +92,7 @@ public function test_does_not_match_non_component_html() { $this->assertCount( 1, $translations ); } - public function test_extracts_context_function_in_prop() { + public function test_extracts_context_function_in_prop(): void { $translations = $this->extract( '' ); @@ -101,7 +101,7 @@ public function test_extracts_context_function_in_prop() { $this->assertNotNull( $translation ); } - public function test_extracts_esc_functions_in_props() { + public function test_extracts_esc_functions_in_props(): void { $blade = <<<'BLADE' assertNotNull( $translations->find( null, 'Enter username' ) ); } - public function test_extracts_single_quoted_bound_props() { + public function test_extracts_single_quoted_bound_props(): void { $translations = $this->extract( "" ); @@ -123,7 +123,7 @@ public function test_extracts_single_quoted_bound_props() { $this->assertNotNull( $translations->find( null, 'Single quoted' ) ); } - public function test_existing_blade_extraction_still_works() { + public function test_existing_blade_extraction_still_works(): void { $blade = <<<'BLADE' @php __('PHP block string', 'foo-theme'); diff --git a/tests/FileDataExtractorTest.php b/tests/FileDataExtractorTest.php index f2663eb4..d6c6f4b7 100644 --- a/tests/FileDataExtractorTest.php +++ b/tests/FileDataExtractorTest.php @@ -6,7 +6,7 @@ use WP_CLI\Tests\TestCase; class FileDataExtractorTest extends TestCase { - public function test_extracts_headers_with_line_numbers() { + public function test_extracts_headers_with_line_numbers(): void { $text = <<<'TEXT' assertEquals( 5, $headers['Version']['line'] ); } - public function test_line_numbers_with_different_line_endings() { + public function test_line_numbers_with_different_line_endings(): void { // Test with different line positions $text = "assertEquals( 6, $headers['Description']['line'] ); } - public function test_empty_headers_with_line_numbers() { + public function test_empty_headers_with_line_numbers(): void { $text = 'assertEquals( 0, $headers['Plugin Name']['line'] ); } - public function test_theme_headers_with_line_numbers() { + public function test_theme_headers_with_line_numbers(): void { $text = <<<'TEXT' /* Theme Name: My Theme @@ -105,7 +105,7 @@ public function test_theme_headers_with_line_numbers() { $this->assertEquals( 5, $headers['Version']['line'] ); } - public function test_header_with_trailing_comment() { + public function test_header_with_trailing_comment(): void { $text = <<<'TEXT' assertContains( $expected, $result ); - $expected = static::$base . 'baz/includes/should_be_included.js'; + $expected = self::$base . 'baz/includes/should_be_included.js'; $this->assertContains( $expected, $result ); $expected = 'hoge/should_NOT_be_included.js'; $this->assertNotContains( $expected, $result ); } - public function test_can_include_empty_array() { + public function test_can_include_empty_array(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [], [], [ 'php', 'js' ] ); - $expected_1 = static::$base . 'foo-plugin/foo-plugin.php'; - $expected_2 = static::$base . 'baz/includes/should_be_included.js'; + $expected_1 = self::$base . 'foo-plugin/foo-plugin.php'; + $expected_2 = self::$base . 'baz/includes/should_be_included.js'; $this->assertContains( $expected_1, $result ); $this->assertContains( $expected_2, $result ); } - public function test_can_include_wildcard() { + public function test_can_include_wildcard(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ '*' ], [], [ 'php', 'js' ] ); - $expected_1 = static::$base . 'foo-plugin/foo-plugin.php'; - $expected_2 = static::$base . 'baz/includes/should_be_included.js'; + $expected_1 = self::$base . 'foo-plugin/foo-plugin.php'; + $expected_2 = self::$base . 'baz/includes/should_be_included.js'; $this->assertContains( $expected_1, $result ); $this->assertContains( $expected_2, $result ); } - public function test_can_include_subdirectories() { + public function test_can_include_subdirectories(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [], [ 'php', 'js' ] ); - $expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; - $expected_2 = static::$base . 'foo/bar/foofoo/included.js'; + $expected_1 = self::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; + $expected_2 = self::$base . 'foo/bar/foofoo/included.js'; $this->assertContains( $expected_1, $result ); $this->assertContains( $expected_2, $result ); } - public function test_can_include_only_php() { + public function test_can_include_only_php(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [], [ 'php' ] ); - $expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; - $expected_2 = static::$base . 'foo/bar/foofoo/ignored.js'; + $expected_1 = self::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; + $expected_2 = self::$base . 'foo/bar/foofoo/ignored.js'; $this->assertContains( $expected_1, $result ); $this->assertNotContains( $expected_2, $result ); } - public function test_can_exclude_override_wildcard() { + public function test_can_exclude_override_wildcard(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'foo/bar/excluded/*' ], [ 'php' ] ); - $expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; - $expected_2 = static::$base . 'foo/bar/excluded/excluded.js'; + $expected_1 = self::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; + $expected_2 = self::$base . 'foo/bar/excluded/excluded.js'; $this->assertContains( $expected_1, $result ); $this->assertNotContains( $expected_2, $result ); } - public function test_can_exclude_override_matching_directory() { + public function test_can_exclude_override_matching_directory(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'foo/bar/excluded/*' ], [ 'php' ] ); - $expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; - $expected_2 = static::$base . 'foo/bar/excluded/excluded.js'; + $expected_1 = self::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; + $expected_2 = self::$base . 'foo/bar/excluded/excluded.js'; $this->assertContains( $expected_1, $result ); $this->assertNotContains( $expected_2, $result ); } - public function test_can_not_exclude_partially_directory() { + public function test_can_not_exclude_partially_directory(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ 'foo/bar/*' ], [ 'exc' ], [ 'js' ] ); - $expected_1 = static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; - $expected_2 = static::$base . 'foo/bar/excluded/ignored.js'; + $expected_1 = self::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php'; + $expected_2 = self::$base . 'foo/bar/excluded/ignored.js'; $this->assertNotContains( $expected_1, $result ); $this->assertContains( $expected_2, $result ); } - public function test_can_exclude_by_wildcard() { + public function test_can_exclude_by_wildcard(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [], [ '*' ], [ 'php', 'js' ] ); $this->assertEmpty( $result ); } - public function test_can_exclude_files() { + public function test_can_exclude_files(): void { $excludes = [ 'hoge' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [], $excludes, [ 'php', 'js' ] ); - $expected = static::$base . 'hoge/should_NOT_be_included.js'; + $expected = self::$base . 'hoge/should_NOT_be_included.js'; $this->assertNotContains( $expected, $result ); } - public function test_can_override_exclude_by_include() { + public function test_can_override_exclude_by_include(): void { // Overrides include option $includes = [ 'excluded/ignored.js' ]; $excludes = [ 'excluded/*.js' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] ); - $expected = static::$base . 'foo/bar/excluded/ignored.js'; + $expected = self::$base . 'foo/bar/excluded/ignored.js'; $this->assertContains( $expected, $result ); } - public function test_can_return_all_directory_files_sorted() { + public function test_can_return_all_directory_files_sorted(): void { $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, [ '*' ], [], [ 'php', 'blade.php', 'js' ] ); $expected = array( - static::$base . 'baz/includes/should_be_included.js', - static::$base . 'foo-plugin/foo-plugin.php', - static::$base . 'foo-theme/foo-theme-file.blade.php', - static::$base . 'foo/bar/excluded/ignored.js', - static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php', - static::$base . 'foo/bar/foofoo/included.js', - static::$base . 'foo/bar/foofoo/minified.min.js', - static::$base . 'hoge/should_NOT_be_included.js', - static::$base . 'vendor/vendor-file.php', - static::$base . 'vendor/vendor1/vendor1-file.php', + self::$base . 'baz/includes/should_be_included.js', + self::$base . 'foo-plugin/foo-plugin.php', + self::$base . 'foo-theme/foo-theme-file.blade.php', + self::$base . 'foo/bar/excluded/ignored.js', + self::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php', + self::$base . 'foo/bar/foofoo/included.js', + self::$base . 'foo/bar/foofoo/minified.min.js', + self::$base . 'hoge/should_NOT_be_included.js', + self::$base . 'vendor/vendor-file.php', + self::$base . 'vendor/vendor1/vendor1-file.php', ); $this->assertEquals( $expected, $result ); } - public function test_can_include_file_in_excluded_folder() { + public function test_can_include_file_in_excluded_folder(): void { $includes = [ 'vendor/vendor-file.php' ]; $excludes = [ 'vendor' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] ); - $expected = static::$base . 'vendor/vendor-file.php'; + $expected = self::$base . 'vendor/vendor-file.php'; $this->assertContains( $expected, $result ); } - public function test_can_include_folder_in_excluded_folder() { + public function test_can_include_folder_in_excluded_folder(): void { $includes = [ 'vendor/vendor1' ]; $excludes = [ 'vendor' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] ); - $expected = static::$base . 'vendor/vendor1/vendor1-file.php'; + $expected = self::$base . 'vendor/vendor1/vendor1-file.php'; $this->assertContains( $expected, $result ); } - public function test_can_include_file_in_excluded_folder_with_leading_slash() { + public function test_can_include_file_in_excluded_folder_with_leading_slash(): void { $includes = [ '/vendor/vendor-file.php' ]; $excludes = [ 'vendor' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] ); - $expected = static::$base . 'vendor/vendor-file.php'; + $expected = self::$base . 'vendor/vendor-file.php'; $this->assertContains( $expected, $result ); } - public function test_can_include_file_in_excluded_folder_by_wildcard() { + public function test_can_include_file_in_excluded_folder_by_wildcard(): void { $includes = [ 'vendor/**' ]; $excludes = [ 'vendor' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] ); - $expected = static::$base . 'vendor/vendor-file.php'; + $expected = self::$base . 'vendor/vendor-file.php'; $this->assertContains( $expected, $result ); } - public function test_exclude_not_included_files() { + public function test_exclude_not_included_files(): void { $includes = [ 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] ); $expected = array( - static::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php', + self::$base . 'foo/bar/foo/bar/foo/bar/deep_directory_also_included.php', ); $this->assertEquals( $expected, $result ); } - public function test_wildcard_exclude() { + public function test_wildcard_exclude(): void { $includes = [ 'foofoo/*' ]; $excludes = [ '*.min.js' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] ); $expected = array( - static::$base . 'foo/bar/foofoo/included.js', + self::$base . 'foo/bar/foofoo/included.js', ); $this->assertEquals( $expected, $result ); } - public function test_identical_include_exclude() { + public function test_identical_include_exclude(): void { $includes = [ '*.min.js' ]; $excludes = [ '*.min.js' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, $excludes, [ 'php', 'js' ] ); @@ -203,15 +203,21 @@ public function test_identical_include_exclude() { $this->assertEquals( $expected, $result ); } - public function test_can_include_file_in_symlinked_folder() { + public function test_can_include_file_in_symlinked_folder(): void { symlink( self::$base . '/baz', self::$base . '/symlinked' ); $includes = [ 'symlinked/includes/should_be_included.js' ]; $result = TestIterableCodeExtractor::getFilesFromDirectory( self::$base, $includes, [], [ 'php', 'js' ] ); - $expected = static::$base . 'symlinked/includes/should_be_included.js'; + $expected = self::$base . 'symlinked/includes/should_be_included.js'; $this->assertContains( $expected, $result ); } - // IterableCodeExtractor::file_get_extension_multi is a private method + /** + * IterableCodeExtractor::file_get_extension_multi is a private method. + * + * @param class-string $class_name Class name. + * @param string $method_name Method name. + * @return \ReflectionMethod + */ protected static function get_method_as_public( $class_name, $method_name ) { $class = new \ReflectionClass( $class_name ); $method = $class->getMethod( $method_name ); @@ -221,11 +227,20 @@ protected static function get_method_as_public( $class_name, $method_name ) { return $method; } + /** + * @param \SplFileInfo $file File. + * @return mixed + */ protected static function file_get_extension_multi_invoke( $file ) { $file_get_extension_multi_method = static::get_method_as_public( TestIterableCodeExtractor::class, 'file_get_extension_multi' ); return $file_get_extension_multi_method->invokeArgs( null, [ $file ] ); } + /** + * @param \SplFileInfo $file File. + * @param array $extensions File extensions to match. + * @return mixed + */ protected static function file_has_file_extension_invoke( $file, $extensions ) { $file_get_extension_multi_method = static::get_method_as_public( TestIterableCodeExtractor::class, 'file_has_file_extension' ); return $file_get_extension_multi_method->invokeArgs( null, [ $file, $extensions ] ); @@ -233,12 +248,18 @@ protected static function file_has_file_extension_invoke( $file, $extensions ) { /** * @dataProvider file_extension_extract_provider + * + * @param string $rel_input_file Input file, relative to the data directory. + * @param string $expected_extension Expected file extension. */ #[DataProvider( 'file_extension_extract_provider' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound - public function test_gets_file_extension_correctly( $rel_input_file, $expected_extension ) { + public function test_gets_file_extension_correctly( $rel_input_file, $expected_extension ): void { $this->assertEquals( static::file_get_extension_multi_invoke( new \SplFileObject( self::$base . $rel_input_file ) ), $expected_extension ); } + /** + * @return array + */ public static function file_extension_extract_provider() { return [ [ 'foo/bar/foofoo/included.js', 'js' ], @@ -249,12 +270,19 @@ public static function file_extension_extract_provider() { /** * @dataProvider file_extensions_matches_provider + * + * @param string $rel_input_file Input file, relative to the data directory. + * @param array $matching_extensions File extensions to match. + * @param bool $expected_result Expected result. */ #[DataProvider( 'file_extensions_matches_provider' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound - public function test_matches_file_extensions_correctly( $rel_input_file, $matching_extensions, $expected_result ) { + public function test_matches_file_extensions_correctly( $rel_input_file, $matching_extensions, $expected_result ): void { $this->assertEquals( static::file_has_file_extension_invoke( new \SplFileObject( self::$base . $rel_input_file ), $matching_extensions ), $expected_result ); } + /** + * @return array, bool}> + */ public static function file_extensions_matches_provider() { return [ [ 'foo/bar/foofoo/included.js', [ 'js' ], true ], diff --git a/tests/JsFunctionsScannerTest.php b/tests/JsFunctionsScannerTest.php index 2d0b1e31..7adb46cc 100644 --- a/tests/JsFunctionsScannerTest.php +++ b/tests/JsFunctionsScannerTest.php @@ -23,7 +23,7 @@ private function extract( $code, array $options = [] ) { return $translations; } - public function test_does_not_extract_anything_without_functions() { + public function test_does_not_extract_anything_without_functions(): void { $translations = $this->extract( "__( 'Hello', 'foo-plugin' );", [ 'functions' => [] ] @@ -32,7 +32,7 @@ public function test_does_not_extract_anything_without_functions() { $this->assertCount( 0, $translations ); } - public function test_ignores_comments_if_comment_extraction_is_disabled() { + public function test_ignores_comments_if_comment_extraction_is_disabled(): void { $translations = $this->extract( "// translators: A comment.\n__( 'Hello', 'foo-plugin' );", [ 'extractComments' => false ] @@ -43,7 +43,7 @@ public function test_ignores_comments_if_comment_extraction_is_disabled() { $this->assertSame( [], $translation->getExtractedComments()->toArray() ); } - public function test_extracts_all_comments_with_empty_prefix() { + public function test_extracts_all_comments_with_empty_prefix(): void { $translations = $this->extract( "// Any comment.\n__( 'Hello', 'foo-plugin' );", [ 'extractComments' => '' ] @@ -54,7 +54,7 @@ public function test_extracts_all_comments_with_empty_prefix() { $this->assertSame( [ 'Any comment.' ], $translation->getExtractedComments()->toArray() ); } - public function test_extracts_translator_comments_with_prefix() { + public function test_extracts_translator_comments_with_prefix(): void { $translations = $this->extract( "// translators: A comment.\n__( 'Hello', 'foo-plugin' );", [ 'extractComments' => [ 'translators', 'Translators' ] ] @@ -65,7 +65,7 @@ public function test_extracts_translator_comments_with_prefix() { $this->assertSame( [ 'translators: A comment.' ], $translation->getExtractedComments()->toArray() ); } - public function test_extracts_calls_with_unicode_escaped_names() { + public function test_extracts_calls_with_unicode_escaped_names(): void { $translations = $this->extract( "\\u005f\\u005f( 'Escaped', 'foo-plugin' );\n_\\u{78}( 'Braced', 'context', 'foo-plugin' );" ); @@ -74,7 +74,7 @@ public function test_extracts_calls_with_unicode_escaped_names() { $this->assertNotNull( $translations->find( 'context', 'Braced' ) ); } - public function test_skips_code_with_unrelated_unicode_escapes() { + public function test_skips_code_with_unrelated_unicode_escapes(): void { $translations = $this->extract( "var s = '\\u005fa'; var t = '\\u00e9';" ); $this->assertCount( 0, $translations ); diff --git a/tests/MakeJsonMapTest.php b/tests/MakeJsonMapTest.php index c24d3e50..14d69512 100644 --- a/tests/MakeJsonMapTest.php +++ b/tests/MakeJsonMapTest.php @@ -5,7 +5,7 @@ use WP_CLI\I18n\MakeJsonCommand; use WP_CLI\Tests\TestCase; -use WP_CLI\Utils; +use WP_CLI\Path; class MakeJsonMapTest extends TestCase { /** @var string A path files are located */ @@ -22,7 +22,7 @@ public function set_up() { * PHP5.4 cannot set property with __DIR__ constant. * Shamelessly stolen from @see IterableCodeExtractorTest.php */ - self::$base = Utils\normalize_path( __DIR__ ) . '/data/'; + self::$base = Path::normalize( __DIR__ ) . '/data/'; self::$obj = new MakeJsonCommand(); $reflection = new \ReflectionClass( get_class( self::$obj ) ); @@ -32,20 +32,20 @@ public function set_up() { } } - public function test_no_map() { + public function test_no_map(): void { $result = self::$build_map->invoke( self::$obj, false ); $expected = null; $this->assertEquals( $expected, $result ); } - public function test_invalid_map() { + public function test_invalid_map(): void { $maps = self::$base . 'maps/invalid.json'; $result = self::$build_map->invoke( self::$obj, $maps ); $expected = []; $this->assertEquals( $expected, $result ); } - public function test_basic_map() { + public function test_basic_map(): void { $maps = self::$base . 'maps/basic.json'; $result = self::$build_map->invoke( self::$obj, $maps ); $expected = [ @@ -55,7 +55,7 @@ public function test_basic_map() { $this->assertEquals( $expected, $result ); } - public function test_mixed_map() { + public function test_mixed_map(): void { $maps = self::$base . 'maps/mixed.json'; $result = self::$build_map->invoke( self::$obj, $maps ); $expected = [ @@ -65,7 +65,7 @@ public function test_mixed_map() { $this->assertEquals( $expected, $result ); } - public function test_other_map() { + public function test_other_map(): void { $maps = self::$base . 'maps/other.json'; $result = self::$build_map->invoke( self::$obj, $maps ); $expected = [ @@ -75,7 +75,7 @@ public function test_other_map() { $this->assertEquals( $expected, $result ); } - public function test_invalid_values_map() { + public function test_invalid_values_map(): void { $maps = self::$base . 'maps/invalid_values.json'; $result = self::$build_map->invoke( self::$obj, $maps ); $expected = [ @@ -86,7 +86,7 @@ public function test_invalid_values_map() { $this->assertEquals( $expected, $result ); } - public function test_merge_map() { + public function test_merge_map(): void { $maps = [ self::$base . 'maps/basic.json', self::$base . 'maps/mixed.json' ]; $result = self::$build_map->invoke( self::$obj, $maps ); $expected = [ @@ -98,7 +98,7 @@ public function test_merge_map() { $this->assertEquals( $expected, $result ); } - public function test_merge_same_map() { + public function test_merge_same_map(): void { $maps = [ self::$base . 'maps/basic.json', self::$base . 'maps/mixed.json', self::$base . 'maps/other.json' ]; $result = self::$build_map->invoke( self::$obj, $maps ); $expected = [ @@ -109,7 +109,7 @@ public function test_merge_same_map() { $this->assertEquals( $expected, $result ); } - public function test_merge_invalid_values_map() { + public function test_merge_invalid_values_map(): void { // merge both ways $maps = [ self::$base . 'maps/basic.json', self::$base . 'maps/invalid_values.json' ]; $result = self::$build_map->invoke( self::$obj, $maps ); diff --git a/tests/PhpFunctionsScannerTest.php b/tests/PhpFunctionsScannerTest.php index 5d8c1f5d..fb51435c 100644 --- a/tests/PhpFunctionsScannerTest.php +++ b/tests/PhpFunctionsScannerTest.php @@ -10,7 +10,7 @@ class PhpFunctionsScannerTest extends TestCase { - public function test_finds_functions_with_arguments_and_comments() { + public function test_finds_functions_with_arguments_and_comments(): void { $code = <<<'PHP' assertSame( [ 'Before', 'After' ], $strings ); } - public function test_joins_lines_of_multiline_comments() { + public function test_joins_lines_of_multiline_comments(): void { $code = <<<'PHP' assertSame( [ 'translators: %s: Name of the person.' ], $translation->getExtractedComments()->toArray() ); } - public function test_extracts_strings_for_text_domain() { + public function test_extracts_strings_for_text_domain(): void { $code = <<<'PHP' insert( null, '%d cat', '%d cats' ); diff --git a/tests/TranslationsTest.php b/tests/TranslationsTest.php index 2ec24456..709fbd06 100644 --- a/tests/TranslationsTest.php +++ b/tests/TranslationsTest.php @@ -24,13 +24,16 @@ public function set_up() { } public function tear_down() { - array_map( 'unlink', (array) glob( $this->dir . '/*' ) ); + $files = glob( $this->dir . '/*' ); + if ( false !== $files ) { + array_map( 'unlink', $files ); + } rmdir( $this->dir ); parent::tear_down(); } - public function test_loads_strings_and_translations_that_are_zero() { + public function test_loads_strings_and_translations_that_are_zero(): void { file_put_contents( "$this->dir/de_DE.po", <<<'PO' @@ -59,7 +62,7 @@ public function test_loads_strings_and_translations_that_are_zero() { $this->assertSame( '0', $translation->getTranslation() ); } - public function test_loads_multiple_files_into_one_set() { + public function test_loads_multiple_files_into_one_set(): void { file_put_contents( "$this->dir/a.pot", "#: a.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n" ); file_put_contents( "$this->dir/b.pot", "#: b.php:1\nmsgid \"Hello\"\nmsgstr \"\"\n\nmsgid \"World\"\nmsgstr \"\"\n" ); @@ -78,7 +81,7 @@ public function test_loads_multiple_files_into_one_set() { ); } - public function test_merges_duplicate_entries() { + public function test_merges_duplicate_entries(): void { $translations = Translations::fromPoString( <<<'PO' msgid "" @@ -116,7 +119,7 @@ public function test_merges_duplicate_entries() { ); } - public function test_merges_duplicate_entries_separated_by_empty_comment() { + public function test_merges_duplicate_entries_separated_by_empty_comment(): void { $translations = Translations::fromPoString( <<<'PO' msgid "" @@ -148,13 +151,13 @@ public function test_merges_duplicate_entries_separated_by_empty_comment() { ); } - public function test_throws_for_files_that_cannot_be_read() { + public function test_throws_for_files_that_cannot_be_read(): void { $this->expectException( RuntimeException::class ); Translations::fromPoFiles( "$this->dir/missing.po" ); } - public function test_keeps_file_comments_and_flags() { + public function test_keeps_file_comments_and_flags(): void { file_put_contents( "$this->dir/de_DE.po", <<<'PO' @@ -182,7 +185,7 @@ public function test_keeps_file_comments_and_flags() { $this->assertStringStartsWith( "# Copyright (C) 2026 Foo Plugin\n#\n#, fuzzy\nmsgid \"\"\n", $result ); } - public function test_insert_returns_existing_translation() { + public function test_insert_returns_existing_translation(): void { $translations = Translations::create(); $first = $translations->insert( 'context', 'Hello' ); @@ -197,7 +200,7 @@ public function test_insert_returns_existing_translation() { $this->assertSame( [ [ 'a.php', 1 ] ], Translations::getReferenceList( $first ) ); } - public function test_translations_have_default_headers() { + public function test_translations_have_default_headers(): void { $translations = Translations::create( 'foo-plugin' ); $this->assertSame( @@ -218,7 +221,7 @@ public function test_translations_have_default_headers() { ); } - public function test_mo_file_includes_headers_and_translations_that_are_zero() { + public function test_mo_file_includes_headers_and_translations_that_are_zero(): void { $translations = Translations::create(); $translations->getHeaders()->set( 'Plural-Forms', 'nplurals=2; plural=(n != 1);' ); diff --git a/tests/includes/TestIterableCodeExtractor.php b/tests/includes/TestIterableCodeExtractor.php index 2476d105..3b808e08 100644 --- a/tests/includes/TestIterableCodeExtractor.php +++ b/tests/includes/TestIterableCodeExtractor.php @@ -3,7 +3,19 @@ namespace WP_CLI\I18n\Tests; use WP_CLI\I18n\IterableCodeExtractor; +use WP_CLI\I18n\Translations; class TestIterableCodeExtractor { use IterableCodeExtractor; + + /** + * No-op implementation of the method the IterableCodeExtractor trait relies on. + * + * @param string $text The text to extract strings from. + * @param Translations $translations Translations instance to add strings to. + * @param array $options Extraction options. + * @return void + */ + public static function fromString( $text, Translations $translations, array $options = [] ) { + } } From 092cc1667c1a78cd9b0fdd5c59bfd2b0114694ab Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 21:00:29 +0000 Subject: [PATCH 2/2] Mark the setAccessible() calls in the tests as deprecated on purpose They only run on PHP < 8.1, where they are still needed, but PHPStan reports them as deprecated when it runs on newer PHP versions. This uses the same comment as the wp-cli/wp-cli tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5n3ZupvgHr4fEBJCGp --- tests/IterableCodeExtractorTest.php | 3 +++ tests/MakeJsonMapTest.php | 1 + 2 files changed, 4 insertions(+) diff --git a/tests/IterableCodeExtractorTest.php b/tests/IterableCodeExtractorTest.php index a6b336b0..638a8500 100644 --- a/tests/IterableCodeExtractorTest.php +++ b/tests/IterableCodeExtractorTest.php @@ -23,10 +23,12 @@ public function set_up() { $property = new \ReflectionProperty( TestIterableCodeExtractor::class, 'dir' ); if ( PHP_VERSION_ID < 80100 ) { + // @phpstan-ignore method.deprecated $property->setAccessible( true ); } $property->setValue( null, self::$base ); if ( PHP_VERSION_ID < 80100 ) { + // @phpstan-ignore method.deprecated $property->setAccessible( false ); } } @@ -222,6 +224,7 @@ protected static function get_method_as_public( $class_name, $method_name ) { $class = new \ReflectionClass( $class_name ); $method = $class->getMethod( $method_name ); if ( PHP_VERSION_ID < 80100 ) { + // @phpstan-ignore method.deprecated $method->setAccessible( true ); } return $method; diff --git a/tests/MakeJsonMapTest.php b/tests/MakeJsonMapTest.php index 14d69512..3abf19d6 100644 --- a/tests/MakeJsonMapTest.php +++ b/tests/MakeJsonMapTest.php @@ -28,6 +28,7 @@ public function set_up() { $reflection = new \ReflectionClass( get_class( self::$obj ) ); self::$build_map = $reflection->getMethod( 'build_map' ); if ( PHP_VERSION_ID < 80100 ) { + // @phpstan-ignore method.deprecated self::$build_map->setAccessible( true ); } }