From c5664add78772140deb6edf0a9ccecf43d407f8e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 18 Aug 2026 18:27:25 +0400 Subject: [PATCH 1/2] Add failing tests for deprecated-file detection on import --- .../phpunit/tests/import/deprecated-file.inc | 14 ++ .../phpunit/tests/import/deprecated-file.php | 134 ++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 tests/phpunit/tests/import/deprecated-file.inc create mode 100644 tests/phpunit/tests/import/deprecated-file.php diff --git a/tests/phpunit/tests/import/deprecated-file.inc b/tests/phpunit/tests/import/deprecated-file.inc new file mode 100644 index 0000000..cdcd28c --- /dev/null +++ b/tests/phpunit/tests/import/deprecated-file.inc @@ -0,0 +1,14 @@ +export_data; + $file_data['uses']['functions'] = $functions_used; + + $importer = new \WP_Parser\Importer; + $importer->import( array( $file_data ) ); + + $posts = get_posts( + array( + 'post_type' => $importer->post_type_function, + 'name' => 'wp_parser_deprecated_file_test_func', + ) + ); + + $this->assertCount( 1, $posts ); + + return get_post_meta( $posts[0]->ID, '_wp-parser_tags', true ); + } + + /** + * Test that a file deprecated by its first call is detected. + */ + public function test_deprecating_first_call_is_detected() { + + $tags = $this->import_with_file_uses( + array( + array( + 'name' => '_deprecated_file', + 'line' => 3, + 'end_line' => 3, + 'deprecation_version' => '2.0.0', + ), + ) + ); + + $this->assertArrayHasKey( 'deprecated', $tags ); + $this->assertSame( '2.0.0', $tags['deprecated'] ); + } + + /** + * Test that the deprecating call is detected after preceding calls. + */ + public function test_deprecating_call_after_other_calls_is_detected() { + + $tags = $this->import_with_file_uses( + array( + array( + 'name' => 'do_something_first', + 'line' => 3, + 'end_line' => 3, + ), + array( + 'name' => '_deprecated_file', + 'line' => 4, + 'end_line' => 4, + 'deprecation_version' => '2.0.0', + ), + ) + ); + + $this->assertArrayHasKey( 'deprecated', $tags ); + $this->assertSame( '2.0.0', $tags['deprecated'] ); + } + + /** + * Test that a file without a deprecating call is not deprecated. + */ + public function test_file_without_deprecating_call_is_not_deprecated() { + + $tags = $this->import_with_file_uses( + array( + array( + 'name' => 'do_something_first', + 'line' => 3, + 'end_line' => 3, + ), + ) + ); + + $this->assertArrayNotHasKey( 'deprecated', $tags ); + } + + /** + * Test that a deprecating call without a version does not deprecate. + */ + public function test_deprecating_call_without_version_is_not_deprecated() { + + $tags = $this->import_with_file_uses( + array( + array( + 'name' => '_deprecated_file', + 'line' => 3, + 'end_line' => 3, + 'deprecation_version' => null, + ), + ) + ); + + $this->assertArrayNotHasKey( 'deprecated', $tags ); + } + + /** + * Test that a file with an empty uses list imports without error. + */ + public function test_empty_uses_list_is_not_deprecated() { + + $tags = $this->import_with_file_uses( array() ); + + $this->assertArrayNotHasKey( 'deprecated', $tags ); + } +} From eb640c89fb2cdc9c6127419eed929358fbdecd17 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 18 Aug 2026 18:27:25 +0400 Subject: [PATCH 2/2] Detect the _deprecated_file() call anywhere in a file's uses --- lib/class-importer.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/class-importer.php b/lib/class-importer.php index c1c264e..c0c37f3 100644 --- a/lib/class-importer.php +++ b/lib/class-importer.php @@ -297,13 +297,18 @@ public function import_file( array $file, $skip_sleep = false, $import_ignored = // Detect deprecated file $deprecated_file = false; if ( isset( $file['uses']['functions'] ) ) { - $first_function = $file['uses']['functions'][0]; + foreach ( $file['uses']['functions'] as $function_use ) { - // If the first function in this file is _deprecated_function - if ( '_deprecated_file' === $first_function['name'] ) { + // The file is deprecated if it calls _deprecated_file() + if ( isset( $function_use['name'] ) && '_deprecated_file' === $function_use['name'] ) { - // Set the deprecated flag to the version number - $deprecated_file = $first_function['deprecation_version']; + // Set the deprecated flag to the version number + if ( isset( $function_use['deprecation_version'] ) ) { + $deprecated_file = $function_use['deprecation_version']; + } + + break; + } } }