Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison with TomasVotruba/unused-public #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions compare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env php
<?php

// usage: vendor/bin/phpstan analyse -v -c compare.phpstan.neon --error-format=prettyJson tests/Rule/data/DeadMethodRule/*.php | php compare.php

$errors = json_decode(file_get_contents("php://stdin"), true);

function transformMessage(string $message): string
{
$replace = [
'Public method "' => '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));
}
8 changes: 8 additions & 0 deletions compare.phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- vendor/tomasvotruba/unused-public/config/extension.neon

parameters:
customRulesetUsed: true

unused_public:
methods: true
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": {
117 changes: 116 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/Rule/data/DeadMethodRule/array-map-1.php
Original file line number Diff line number Diff line change
@@ -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 {}

}

8 changes: 4 additions & 4 deletions tests/Rule/data/DeadMethodRule/basic.php
Original file line number Diff line number Diff line change
@@ -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
{

}
2 changes: 1 addition & 1 deletion tests/Rule/data/DeadMethodRule/class-string.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

class ClassWithMethod {

public static function someMethod(): void {}
public static function someMethod(): void {} // error: Unused ClassStringCall\ClassWithMethod::someMethod
}

/**
2 changes: 1 addition & 1 deletion tests/Rule/data/DeadMethodRule/ctor-interface.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

interface MyInterface
{
public function __construct(); // error: Unused CtorInterface\MyInterface::__construct
public function __construct();
}

class Child1 implements MyInterface
2 changes: 1 addition & 1 deletion tests/Rule/data/DeadMethodRule/ctor.php
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ public function __construct()

class Child1 extends ParentClass
{
public function __construct() // error: Unused Ctor\Child1::__construct
public function __construct()
{
}
}
2 changes: 1 addition & 1 deletion tests/Rule/data/DeadMethodRule/dead-in-parent-1.php
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ class B extends A
/**
* @inheritDoc
*/
public function __construct() // error: Unused DeadParent\B::__construct
public function __construct()
{
parent::__construct();
}
8 changes: 4 additions & 4 deletions tests/Rule/data/DeadMethodRule/dynamic-method.php
Original file line number Diff line number Diff line change
@@ -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
}

/**
2 changes: 1 addition & 1 deletion tests/Rule/data/DeadMethodRule/entrypoint.php
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ public function __construct()
{
}

public function someUnused(): void
public function someUnused(): void // error: Unused DeadEntrypoint\Entrypoint::someUnused
{
Dead::usedMethod();
}
2 changes: 1 addition & 1 deletion tests/Rule/data/DeadMethodRule/indirect-interface.php
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@

interface FooInterface
{
public function foo(): void; // error: Unused DeadIndirect\FooInterface::foo
public function foo(): void;
}

abstract class FooAbstract
2 changes: 1 addition & 1 deletion tests/Rule/data/DeadMethodRule/nullsafe.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 2 additions & 2 deletions tests/Rule/data/DeadMethodRule/overwriting-methods-1.php
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions tests/Rule/data/DeadMethodRule/overwriting-methods-2.php
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading