From 4ae54c0e76df62b787325150f1a3f457ad4af2ab Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 3 Sep 2024 15:29:15 +0200 Subject: [PATCH 1/2] Compare tests with TomasVotruba/unused-public --- compare.php | 39 +++++++++++++++ compare.phpstan.neon | 8 +++ composer.json | 3 +- composer.lock | 117 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 compare.php create mode 100644 compare.phpstan.neon diff --git a/compare.php b/compare.php new file mode 100644 index 0000000..467f2f1 --- /dev/null +++ b/compare.php @@ -0,0 +1,39 @@ +#!/usr/bin/env php + 'Unused ', + '()" is never used' => '', + ]; + return str_replace(array_keys($replace), array_values($replace), $message); +} + +$iterator = new DirectoryIterator(__DIR__ . '/tests/Rule/data/DeadMethodRule'); + +foreach ($iterator as $fileinfo) { + if (!$fileinfo->isFile() || $fileinfo->getExtension() !== 'php') { + continue; + } + $filePath = $fileinfo->getPathname(); + + $contents = file_get_contents($filePath); + $contentsLines = explode("\n", $contents); + + foreach ($contentsLines as $line => $row) { + $newLine = preg_replace('~ ?// error.*$~', '', $row); + $contentsLines[$line] = $newLine; + } + + foreach ($errors['files'][$filePath]['messages'] ?? [] as $error) { + $line = $error['line']; + $contentsLines[$line - 1] .= ' // error: ' . transformMessage($error['message']); + } + + file_put_contents($filePath, implode("\n", $contentsLines)); +} diff --git a/compare.phpstan.neon b/compare.phpstan.neon new file mode 100644 index 0000000..f95b404 --- /dev/null +++ b/compare.phpstan.neon @@ -0,0 +1,8 @@ +includes: + - vendor/tomasvotruba/unused-public/config/extension.neon + +parameters: + customRulesetUsed: true + + unused_public: + methods: true diff --git a/composer.json b/composer.json index ad7666d..dc500f6 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "slevomat/coding-standard": "^8.15.0", "symfony/contracts": "^2.5 || ^3.0", "symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0", - "symfony/routing": "^5.4 || ^6.0 || ^7.0" + "symfony/routing": "^5.4 || ^6.0 || ^7.0", + "tomasvotruba/unused-public": "^0.3.11" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 710638f..c87bbf0 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": "73654267cbbb625ea912738f32d0a446", + "content-hash": "b882dbf953f555e7afa2830495b1afd3", "packages": [ { "name": "phpstan/phpstan", @@ -5178,6 +5178,121 @@ } ], "time": "2024-03-03T12:36:25+00:00" + }, + { + "name": "tomasvotruba/unused-public", + "version": "0.3.11", + "source": { + "type": "git", + "url": "https://github.com/TomasVotruba/unused-public.git", + "reference": "fa0586e674034961fe8d7f8e8356831f89fea64f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/TomasVotruba/unused-public/zipball/fa0586e674034961fe8d7f8e8356831f89fea64f", + "reference": "fa0586e674034961fe8d7f8e8356831f89fea64f", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.10.19", + "webmozart/assert": "^1.11" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "config/extension.neon" + ] + } + }, + "autoload": { + "psr-4": { + "TomasVotruba\\UnusedPublic\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Detect unused public properties, constants and methods in your code", + "keywords": [ + "phpstan-extension", + "static analysis" + ], + "support": { + "issues": "https://github.com/TomasVotruba/unused-public/issues", + "source": "https://github.com/TomasVotruba/unused-public/tree/0.3.11" + }, + "funding": [ + { + "url": "https://www.paypal.me/rectorphp", + "type": "custom" + }, + { + "url": "https://github.com/tomasvotruba", + "type": "github" + } + ], + "time": "2024-08-29T14:39:35+00:00" + }, + { + "name": "webmozart/assert", + "version": "1.11.0", + "source": { + "type": "git", + "url": "https://github.com/webmozarts/assert.git", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/11cb2199493b2f8a3b53e7f19068fc6aac760991", + "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "php": "^7.2 || ^8.0" + }, + "conflict": { + "phpstan/phpstan": "<0.12.20", + "vimeo/psalm": "<4.6.1 || 4.6.2" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.13" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.10-dev" + } + }, + "autoload": { + "psr-4": { + "Webmozart\\Assert\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + } + ], + "description": "Assertions to validate method input/output with nice error messages.", + "keywords": [ + "assert", + "check", + "validate" + ], + "support": { + "issues": "https://github.com/webmozarts/assert/issues", + "source": "https://github.com/webmozarts/assert/tree/1.11.0" + }, + "time": "2022-06-03T18:03:27+00:00" } ], "aliases": [], From 2552afc40f1766c35fe2ca61f89de3fa4eb08372 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 3 Sep 2024 15:29:41 +0200 Subject: [PATCH 2/2] Run compare script to see diffs --- tests/Rule/data/DeadMethodRule/array-map-1.php | 10 +++++----- tests/Rule/data/DeadMethodRule/basic.php | 8 ++++---- tests/Rule/data/DeadMethodRule/class-string.php | 2 +- tests/Rule/data/DeadMethodRule/ctor-interface.php | 2 +- tests/Rule/data/DeadMethodRule/ctor.php | 2 +- tests/Rule/data/DeadMethodRule/dead-in-parent-1.php | 2 +- tests/Rule/data/DeadMethodRule/dynamic-method.php | 8 ++++---- tests/Rule/data/DeadMethodRule/entrypoint.php | 2 +- tests/Rule/data/DeadMethodRule/indirect-interface.php | 2 +- tests/Rule/data/DeadMethodRule/nullsafe.php | 2 +- .../Rule/data/DeadMethodRule/overwriting-methods-1.php | 4 ++-- .../Rule/data/DeadMethodRule/overwriting-methods-2.php | 8 ++++---- .../Rule/data/DeadMethodRule/overwriting-methods-3.php | 8 ++++---- .../Rule/data/DeadMethodRule/overwriting-methods-4.php | 2 +- .../Rule/data/DeadMethodRule/overwriting-methods-5.php | 2 +- tests/Rule/data/DeadMethodRule/parent-call-1.php | 2 +- tests/Rule/data/DeadMethodRule/parent-call-2.php | 2 +- tests/Rule/data/DeadMethodRule/parent-call-4.php | 2 +- tests/Rule/data/DeadMethodRule/traits-1.php | 2 +- tests/Rule/data/DeadMethodRule/traits-3.php | 4 ++-- tests/Rule/data/DeadMethodRule/traits-4.php | 2 +- tests/Rule/data/DeadMethodRule/traits-5.php | 2 +- tests/Rule/data/DeadMethodRule/traits-7.php | 2 +- tests/Rule/data/DeadMethodRule/traits-8.php | 2 +- 24 files changed, 42 insertions(+), 42 deletions(-) diff --git a/tests/Rule/data/DeadMethodRule/array-map-1.php b/tests/Rule/data/DeadMethodRule/array-map-1.php index f795204..b8b9fde 100644 --- a/tests/Rule/data/DeadMethodRule/array-map-1.php +++ b/tests/Rule/data/DeadMethodRule/array-map-1.php @@ -15,10 +15,10 @@ public function __construct() } public function method1(string $foo): void {} // error: Unused DeadMap\ArrayMapTest::method1 - public function method2(): void {} - public function method3(): void {} - public function method4(): void {} - public static function method5(): void {} + public function method2(): void {} // error: Unused DeadMap\ArrayMapTest::method2 + public function method3(): void {} // error: Unused DeadMap\ArrayMapTest::method3 + public function method4(): void {} // error: Unused DeadMap\ArrayMapTest::method4 + public static function method5(): void {} // error: Unused DeadMap\ArrayMapTest::method5 public static function method6(): void {} // error: Unused DeadMap\ArrayMapTest::method6 } @@ -28,7 +28,7 @@ public function method2(): void {} public function method3(): void {} public function method4(): void {} public static function method5(): void {} // should be reported (https://github.com/phpstan/phpstan-src/pull/3372) - public static function method6(): void {} // error: Unused DeadMap\Child::method6 + public static function method6(): void {} } diff --git a/tests/Rule/data/DeadMethodRule/basic.php b/tests/Rule/data/DeadMethodRule/basic.php index e78ba58..fd4293b 100644 --- a/tests/Rule/data/DeadMethodRule/basic.php +++ b/tests/Rule/data/DeadMethodRule/basic.php @@ -62,7 +62,7 @@ abstract class TestParent { use TestTrait; - public function __construct() // error: Unused DeadBasic\TestParent::__construct + public function __construct() { } @@ -72,7 +72,7 @@ public function parentMethodUsed(TestChild $child): void $this->traitMethodUsed(); } - public function overwrittenParentMethodUsedByChild(): void // error: Unused DeadBasic\TestParent::overwrittenParentMethodUsedByChild + public function overwrittenParentMethodUsedByChild(): void { } @@ -85,7 +85,7 @@ public function parentMethodUnused(): void // error: Unused DeadBasic\TestParen trait TestTrait { - public function __construct() // error: Unused DeadBasic\TestTrait::__construct + public function __construct() { } @@ -94,7 +94,7 @@ public function traitMethodUsed(): void } - public function traitMethodUnused(): void // error: Unused DeadBasic\TestTrait::traitMethodUnused + public function traitMethodUnused(): void { } diff --git a/tests/Rule/data/DeadMethodRule/class-string.php b/tests/Rule/data/DeadMethodRule/class-string.php index bfcb719..423d64e 100644 --- a/tests/Rule/data/DeadMethodRule/class-string.php +++ b/tests/Rule/data/DeadMethodRule/class-string.php @@ -4,7 +4,7 @@ class ClassWithMethod { - public static function someMethod(): void {} + public static function someMethod(): void {} // error: Unused ClassStringCall\ClassWithMethod::someMethod } /** diff --git a/tests/Rule/data/DeadMethodRule/ctor-interface.php b/tests/Rule/data/DeadMethodRule/ctor-interface.php index 3fec867..ac675fb 100644 --- a/tests/Rule/data/DeadMethodRule/ctor-interface.php +++ b/tests/Rule/data/DeadMethodRule/ctor-interface.php @@ -4,7 +4,7 @@ interface MyInterface { - public function __construct(); // error: Unused CtorInterface\MyInterface::__construct + public function __construct(); } class Child1 implements MyInterface diff --git a/tests/Rule/data/DeadMethodRule/ctor.php b/tests/Rule/data/DeadMethodRule/ctor.php index 8246d39..d57c431 100644 --- a/tests/Rule/data/DeadMethodRule/ctor.php +++ b/tests/Rule/data/DeadMethodRule/ctor.php @@ -11,7 +11,7 @@ public function __construct() class Child1 extends ParentClass { - public function __construct() // error: Unused Ctor\Child1::__construct + public function __construct() { } } diff --git a/tests/Rule/data/DeadMethodRule/dead-in-parent-1.php b/tests/Rule/data/DeadMethodRule/dead-in-parent-1.php index 5d0e3cd..5022eee 100644 --- a/tests/Rule/data/DeadMethodRule/dead-in-parent-1.php +++ b/tests/Rule/data/DeadMethodRule/dead-in-parent-1.php @@ -20,7 +20,7 @@ class B extends A /** * @inheritDoc */ - public function __construct() // error: Unused DeadParent\B::__construct + public function __construct() { parent::__construct(); } diff --git a/tests/Rule/data/DeadMethodRule/dynamic-method.php b/tests/Rule/data/DeadMethodRule/dynamic-method.php index 5ec0d83..3c595c5 100644 --- a/tests/Rule/data/DeadMethodRule/dynamic-method.php +++ b/tests/Rule/data/DeadMethodRule/dynamic-method.php @@ -4,11 +4,11 @@ class Test { - public static function a(): void {} - public static function b(): void {} + public static function a(): void {} // error: Unused DynamicMethod\Test::a + public static function b(): void {} // error: Unused DynamicMethod\Test::b - public function c(): void {} - public function d(): void {} + public function c(): void {} // error: Unused DynamicMethod\Test::c + public function d(): void {} // error: Unused DynamicMethod\Test::d } /** diff --git a/tests/Rule/data/DeadMethodRule/entrypoint.php b/tests/Rule/data/DeadMethodRule/entrypoint.php index ee3d4f1..9f68e80 100644 --- a/tests/Rule/data/DeadMethodRule/entrypoint.php +++ b/tests/Rule/data/DeadMethodRule/entrypoint.php @@ -9,7 +9,7 @@ public function __construct() { } - public function someUnused(): void + public function someUnused(): void // error: Unused DeadEntrypoint\Entrypoint::someUnused { Dead::usedMethod(); } diff --git a/tests/Rule/data/DeadMethodRule/indirect-interface.php b/tests/Rule/data/DeadMethodRule/indirect-interface.php index 8b6a804..d9b963f 100644 --- a/tests/Rule/data/DeadMethodRule/indirect-interface.php +++ b/tests/Rule/data/DeadMethodRule/indirect-interface.php @@ -4,7 +4,7 @@ interface FooInterface { - public function foo(): void; // error: Unused DeadIndirect\FooInterface::foo + public function foo(): void; } abstract class FooAbstract diff --git a/tests/Rule/data/DeadMethodRule/nullsafe.php b/tests/Rule/data/DeadMethodRule/nullsafe.php index 3025b46..b8e0ad7 100644 --- a/tests/Rule/data/DeadMethodRule/nullsafe.php +++ b/tests/Rule/data/DeadMethodRule/nullsafe.php @@ -12,7 +12,7 @@ public function second(): self { return $this; } - public static function secondStatic(): self { + public static function secondStatic(): self { // error: Unused Nullsafe\A::secondStatic return new self(); } } diff --git a/tests/Rule/data/DeadMethodRule/overwriting-methods-1.php b/tests/Rule/data/DeadMethodRule/overwriting-methods-1.php index 86e50e0..d3d0895 100644 --- a/tests/Rule/data/DeadMethodRule/overwriting-methods-1.php +++ b/tests/Rule/data/DeadMethodRule/overwriting-methods-1.php @@ -4,12 +4,12 @@ interface Interface1 { - public function foo(): void; // error: Unused DeadOver1\Interface1::foo + public function foo(): void; } interface Interface2 { - public function foo(): void; // error: Unused DeadOver1\Interface2::foo + public function foo(): void; } abstract class AbstractClass implements Interface1, Interface2 diff --git a/tests/Rule/data/DeadMethodRule/overwriting-methods-2.php b/tests/Rule/data/DeadMethodRule/overwriting-methods-2.php index 6883cfc..8fa17b1 100644 --- a/tests/Rule/data/DeadMethodRule/overwriting-methods-2.php +++ b/tests/Rule/data/DeadMethodRule/overwriting-methods-2.php @@ -4,17 +4,17 @@ interface Interface1 { - public function foo(): void; // error: Unused DeadOver2\Interface1::foo + public function foo(): void; } interface Interface2 { - public function foo(): void; // error: Unused DeadOver2\Interface2::foo + public function foo(): void; } abstract class AbstractClass implements Interface1, Interface2 { - public abstract function foo(): void; // error: Unused DeadOver2\AbstractClass::foo + public abstract function foo(): void; } class Child1 extends AbstractClass @@ -24,7 +24,7 @@ public function foo(): void {} class Child2 extends AbstractClass { - public function foo(): void {} // error: Unused DeadOver2\Child2::foo + public function foo(): void {} } function testIt(Child1 $child1): void diff --git a/tests/Rule/data/DeadMethodRule/overwriting-methods-3.php b/tests/Rule/data/DeadMethodRule/overwriting-methods-3.php index aa2fd08..fd4a0b4 100644 --- a/tests/Rule/data/DeadMethodRule/overwriting-methods-3.php +++ b/tests/Rule/data/DeadMethodRule/overwriting-methods-3.php @@ -4,22 +4,22 @@ interface Interface1 { - public function foo(): void; // error: Unused DeadOver3\Interface1::foo + public function foo(): void; } interface Interface2 { - public function foo(): void; // error: Unused DeadOver3\Interface2::foo + public function foo(): void; } abstract class AbstractClass implements Interface1, Interface2 { - public abstract function foo(): void; // error: Unused DeadOver3\AbstractClass::foo + public abstract function foo(): void; } class Child1 extends AbstractClass { - public function foo(): void {} // error: Unused DeadOver3\Child1::foo + public function foo(): void {} } class Child2 extends AbstractClass diff --git a/tests/Rule/data/DeadMethodRule/overwriting-methods-4.php b/tests/Rule/data/DeadMethodRule/overwriting-methods-4.php index e7fca49..05d0dd3 100644 --- a/tests/Rule/data/DeadMethodRule/overwriting-methods-4.php +++ b/tests/Rule/data/DeadMethodRule/overwriting-methods-4.php @@ -9,7 +9,7 @@ public function foo(): void; interface Interface2 { - public function foo(): void; // error: Unused DeadOver4\Interface2::foo + public function foo(): void; } abstract class AbstractClass implements Interface1, Interface2 diff --git a/tests/Rule/data/DeadMethodRule/overwriting-methods-5.php b/tests/Rule/data/DeadMethodRule/overwriting-methods-5.php index d0b4036..48ffe94 100644 --- a/tests/Rule/data/DeadMethodRule/overwriting-methods-5.php +++ b/tests/Rule/data/DeadMethodRule/overwriting-methods-5.php @@ -4,7 +4,7 @@ interface Interface1 { - public function foo(): void; // error: Unused DeadOver5\Interface1::foo + public function foo(): void; } interface Interface2 diff --git a/tests/Rule/data/DeadMethodRule/parent-call-1.php b/tests/Rule/data/DeadMethodRule/parent-call-1.php index b1c1429..bf175dc 100644 --- a/tests/Rule/data/DeadMethodRule/parent-call-1.php +++ b/tests/Rule/data/DeadMethodRule/parent-call-1.php @@ -19,7 +19,7 @@ public function foo(): void { class Child2 extends AbstractClass { - public function foo(): void {} // error: Unused ParentCall1\Child2::foo + public function foo(): void {} } function testIt(Child1 $child1): void diff --git a/tests/Rule/data/DeadMethodRule/parent-call-2.php b/tests/Rule/data/DeadMethodRule/parent-call-2.php index c460f69..de7ec50 100644 --- a/tests/Rule/data/DeadMethodRule/parent-call-2.php +++ b/tests/Rule/data/DeadMethodRule/parent-call-2.php @@ -4,7 +4,7 @@ abstract class AbstractClass { - public function foo(): void {} // error: Unused ParentCall2\AbstractClass::foo + public function foo(): void {} } class Child1 extends AbstractClass diff --git a/tests/Rule/data/DeadMethodRule/parent-call-4.php b/tests/Rule/data/DeadMethodRule/parent-call-4.php index 6ddf264..9923d12 100644 --- a/tests/Rule/data/DeadMethodRule/parent-call-4.php +++ b/tests/Rule/data/DeadMethodRule/parent-call-4.php @@ -11,7 +11,7 @@ public function __construct() class AbstractClass extends RootClass { - public function __construct() // error: Unused ParentCall4\AbstractClass::__construct + public function __construct() { } } diff --git a/tests/Rule/data/DeadMethodRule/traits-1.php b/tests/Rule/data/DeadMethodRule/traits-1.php index 127b312..6d3e84c 100644 --- a/tests/Rule/data/DeadMethodRule/traits-1.php +++ b/tests/Rule/data/DeadMethodRule/traits-1.php @@ -5,7 +5,7 @@ trait Trait1 { public static function used(): void {} - public static function unused(): void {} // error: Unused DeadTrait1\Trait1::unused + public static function unused(): void {} } class User1 diff --git a/tests/Rule/data/DeadMethodRule/traits-3.php b/tests/Rule/data/DeadMethodRule/traits-3.php index b7695d0..59723cc 100644 --- a/tests/Rule/data/DeadMethodRule/traits-3.php +++ b/tests/Rule/data/DeadMethodRule/traits-3.php @@ -5,12 +5,12 @@ trait MyTrait1 { public function used(): void {} - public function unused(): void {} // error: Unused DeadTrait3\MyTrait1::unused + public function unused(): void {} } interface TraitInterface { - public function used(): void; // error: Unused DeadTrait3\TraitInterface::used + public function used(): void; } class MyUser1 implements TraitInterface diff --git a/tests/Rule/data/DeadMethodRule/traits-4.php b/tests/Rule/data/DeadMethodRule/traits-4.php index c273f87..b7d8c69 100644 --- a/tests/Rule/data/DeadMethodRule/traits-4.php +++ b/tests/Rule/data/DeadMethodRule/traits-4.php @@ -4,7 +4,7 @@ trait MyTrait1 { - public function __construct() // error: Unused DeadTrait4\MyTrait1::__construct + public function __construct() { } } diff --git a/tests/Rule/data/DeadMethodRule/traits-5.php b/tests/Rule/data/DeadMethodRule/traits-5.php index 22bc2fe..a6b2846 100644 --- a/tests/Rule/data/DeadMethodRule/traits-5.php +++ b/tests/Rule/data/DeadMethodRule/traits-5.php @@ -5,7 +5,7 @@ trait MyTrait1 { // because all children override this method, it is unused - public function used(): void {} // error: Unused DeadTrait5\MyTrait1::used + public function used(): void {} } interface TraitInterface diff --git a/tests/Rule/data/DeadMethodRule/traits-7.php b/tests/Rule/data/DeadMethodRule/traits-7.php index 3852761..740f1f2 100644 --- a/tests/Rule/data/DeadMethodRule/traits-7.php +++ b/tests/Rule/data/DeadMethodRule/traits-7.php @@ -3,7 +3,7 @@ namespace DeadTrait7; trait HelloWorld { - public function sayHello() { // error: Unused DeadTrait7\HelloWorld::sayHello + public function sayHello() { echo 'Hello World!'; } } diff --git a/tests/Rule/data/DeadMethodRule/traits-8.php b/tests/Rule/data/DeadMethodRule/traits-8.php index aaf2274..f299f8b 100644 --- a/tests/Rule/data/DeadMethodRule/traits-8.php +++ b/tests/Rule/data/DeadMethodRule/traits-8.php @@ -3,7 +3,7 @@ namespace DeadTrait8; trait A { - public function smallTalk() { // error: Unused DeadTrait8\A::smallTalk + public function smallTalk() { echo 'a'; } public function bigTalk() {