Skip to content

Dead code detection #3581

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
69 changes: 69 additions & 0 deletions build/PHPStan/Build/ApiPhpDocEntrypointProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types = 1);

namespace PHPStan\Build;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use ShipMonk\PHPStan\DeadCode\Provider\SimpleMethodEntrypointProvider;

final class ApiPhpDocEntrypointProvider extends SimpleMethodEntrypointProvider
{

public function isEntrypointMethod(\ReflectionMethod $method): bool
{
$reflectionClass = $method->getDeclaringClass();
$methodName = $method->getName();

if ($this->isApiClass($reflectionClass)) {
return true;
}

do {
if ($this->isApiMethod($reflectionClass, $methodName)) {
return true;
}

foreach ($reflectionClass->getInterfaces() as $interface) {
if ($this->isApiClass($interface)) {
return true;
}

if ($this->isApiMethod($interface, $methodName)) {
return true;
}
}

$reflectionClass = $reflectionClass->getParentClass();
} while ($reflectionClass !== false);

return false;
}

private function isApiClass(\ReflectionClass $reflection): bool

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T

Check failure on line 46 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiClass() has parameter $reflection with generic class ReflectionClass but does not specify its types: T
{
$phpDoc = $reflection->getDocComment();
if ($phpDoc !== false && str_contains($phpDoc, '@api')) {
return true;
}

return false;
}

private function isApiMethod(\ReflectionClass $reflectionClass, string $methodName): bool

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T

Check failure on line 56 in build/PHPStan/Build/ApiPhpDocEntrypointProvider.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\Build\ApiPhpDocEntrypointProvider::isApiMethod() has parameter $reflectionClass with generic class ReflectionClass but does not specify its types: T
{
if (!$reflectionClass->hasMethod($methodName)) {
return false;
}

$phpDoc = $reflectionClass->getMethod($methodName)->getDocComment();
if ($phpDoc !== false && str_contains($phpDoc, '@api')) {
return true;
}

return false;
}
}
9 changes: 9 additions & 0 deletions build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ includes:
- ../vendor/phpstan/phpstan-phpunit/extension.neon
- ../vendor/phpstan/phpstan-phpunit/rules.neon
- ../vendor/phpstan/phpstan-strict-rules/rules.neon
- ../vendor/shipmonk/dead-code-detector/rules.neon
- ../conf/bleedingEdge.neon
- ../phpstan-baseline.neon
- ../phpstan-baseline.php
Expand Down Expand Up @@ -91,6 +92,10 @@ parameters:
path: ../src/Diagnose/PHPStanDiagnoseExtension.php
- '#^Parameter \#1 \$offsetType of class PHPStan\\Type\\Accessory\\HasOffsetType constructor expects PHPStan\\Type\\Constant\\ConstantIntegerType\|PHPStan\\Type\\Constant\\ConstantStringType#'
- '#^Short ternary operator is not allowed#'

- '#^Unused .*?Factory::create$#' # likely used in DIC
- '#^Unused .*?::__construct$#' # disable dead construct analysis, DIC services might not be autowired

reportStaticMethodSignatures: true
tmpDir: %rootDir%/tmp
stubFiles:
Expand All @@ -111,3 +116,7 @@ services:
class: PHPStan\Internal\ContainerDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Build\ApiPhpDocEntrypointProvider
tags:
- shipmonk.deadCode.entrypointProvider
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"phpstan/phpstan-strict-rules": "^1.6",
"phpunit/phpunit": "^9.5.4",
"shipmonk/composer-dependency-analyser": "^1.5",
"shipmonk/dead-code-detector": "dev-master",
"shipmonk/name-collision-detector": "^2.0"
},
"config": {
Expand Down
74 changes: 72 additions & 2 deletions composer.lock

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

Loading