Skip to content

Commit

Permalink
Merge branch 'TomasVotruba-main'
Browse files Browse the repository at this point in the history
* TomasVotruba-main: (21 commits)
  Delete .github/FUNDING.yml
  Update FUNDING.yml
  Update composer.json
  Keep the code DRY
  Small improvement
  Added support for @internal or @public
  Reduce memory consumption of collectors (TomasVotruba#131)
  Fix blade regex to discover method call with args (TomasVotruba#128)
  Fix template discovery, to include root file too (TomasVotruba#127)
  remove `composer-dependency-analyser.php` from releases (TomasVotruba#126)
  Bump deps (TomasVotruba#125)
  Detect public properties used via Subclass (TomasVotruba#123)
  Fix ClassConstFetchCollector (TomasVotruba#122)
  Fix union-type handling in PublicStaticPropertyFetchCollector (TomasVotruba#121)
  Fix union-type handling in PublicPropertyFetchCollector (TomasVotruba#120)
  add phpstan error identifiers (TomasVotruba#118)
  Fixed nette/utils indirect dependency (TomasVotruba#116)
  Added test for JsonSerialize (TomasVotruba#112)
  Add RelativeUnusedPublicClassMethodRule (TomasVotruba#111)
  Bump to PHP 8.2 (TomasVotruba#110)
  ...
  • Loading branch information
neoighodaro committed Nov 21, 2024
2 parents e570935 + 3e2a429 commit d86448a
Show file tree
Hide file tree
Showing 59 changed files with 785 additions and 188 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
name: 'Check Active Classes'
run: vendor/bin/class-leak check src --ansi

-
name: 'Detect composer dependency issues'
run: vendor/bin/composer-dependency-analyser

name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest

Expand All @@ -44,7 +48,7 @@ jobs:
# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.2
coverage: none

# composer install cache - https://github.com/ramsey/composer-install
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
-
uses: "shivammathur/setup-php@v2"
with:
php-version: 8.1
php-version: 8.2
coverage: none

- uses: "ramsey/composer-install@v2"
Expand All @@ -28,7 +28,7 @@ jobs:
- run: cp build/composer-php-72.json composer.json

# clear the dev files
- run: rm -rf build .github tests stubs ecs.php phpstan.neon phpunit.xml
- run: rm -rf build .github tests stubs ecs.php phpstan.neon phpunit.xml composer-dependency-analyser.php

# setup git user
-
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
-
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.2
coverage: none

- uses: "ramsey/composer-install@v2"
Expand Down
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<br>

It's easy to find unused private class elements, because they're not used in the class itself:
It's easy to find unused private class elements, because they're not used in the class itself. But what about public methods/properties/constants?

```diff
final class Book
Expand All @@ -18,14 +18,14 @@ It's easy to find unused private class elements, because they're not used in the
// ...
}

- private function getSubtitle(): string
- public function getSubtitle(): string
- {
- // ...
- }
}
```

But what about public class elements?
**How can we detect unused public element?**

<br>

Expand All @@ -40,7 +40,7 @@ That's exactly what this package does.

<br>

This technique is very useful for private projects and to detect accidentally open public API that should be used only locally.
This technique is very useful for private projects and to detect accidentally used `public` modifier that should be changed to `private` as called locally only.

<br>

Expand All @@ -50,7 +50,7 @@ This technique is very useful for private projects and to detect accidentally op
composer require tomasvotruba/unused-public --dev
```

The package is available on PHP 7.2-8.1 versions in tagged releases.
The package is available for PHP 7.2+ version.

<br>

Expand All @@ -71,6 +71,24 @@ parameters:
<br>
Do you have hundreds of reported public method? You don't have time to check them all, but want to handle them gradually?
Set maximum allowed % configuration instead:
```yaml
# phpstan.neon
parameters:
unused_public:
methods: 2.5
```
This means maximum 2.5 % of all public methods is allowed as unused:
* If it's 5 %, you'll be alerted.
* If it's 1 %, it will be skipped as tolerated.
<br>
Do you want to check local-only method calls that should not be removed, but be turned into `private`/`protected` instead?

```yaml
Expand Down
11 changes: 11 additions & 0 deletions composer-dependency-analyser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;

$config = new Configuration();

return $config
->ignoreErrorsOnPaths([__DIR__ . '/tests'], [ErrorType::UNKNOWN_CLASS]);
23 changes: 12 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
"license": "MIT",
"keywords": ["static analysis", "phpstan-extension"],
"require": {
"php": "^8.1",
"phpstan/phpstan": "^1.10",
"webmozart/assert": "^1.11"
"php": "^8.2",
"phpstan/phpstan": "^1.12",
"webmozart/assert": "^1.11",
"nikic/php-parser": "^4.19"
},
"require-dev": {
"phpstan/extension-installer": "^1.3",
"phpstan/extension-installer": "^1.4",
"tracy/tracy": "^2.10",
"symplify/easy-coding-standard": "^12.0",
"rector/rector": "^1.0",
"symplify/easy-coding-standard": "^12.3",
"rector/rector": "^1.2.5",
"phpunit/phpunit": "^10.5",
"tomasvotruba/class-leak": "^0.2",
"tomasvotruba/cognitive-complexity": "^0.2.2",
"tomasvotruba/type-coverage": "^0.2",
"symplify/easy-ci": "^11.2",
"nikic/php-parser": "^4.19"
"tomasvotruba/class-leak": "^0.2.11",
"tomasvotruba/type-coverage": "^0.3",
"symplify/easy-ci": "^12.1",
"shipmonk/composer-dependency-analyser": "^1.7",
"symplify/phpstan-rules": "^13.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 3 additions & 4 deletions config/extension.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
parametersSchema:
unused_public: structure([
methods: bool()
methods: anyOf(bool(), int(), float())
local_methods: bool()
properties: bool()
constants: bool()
template_paths: listOf(string())
twig_template_paths: listOf(string())
])

# default parameters
Expand All @@ -16,8 +15,6 @@ parameters:
properties: true
constants: true
template_paths: []
# deprecated
twig_template_paths: []

services:
- TomasVotruba\UnusedPublic\PublicClassMethodMatcher
Expand All @@ -30,6 +27,7 @@ services:
- TomasVotruba\UnusedPublic\StmtAnalyzers\RequiredStmtAnalyzer
- TomasVotruba\UnusedPublic\ClassMethodCallReferenceResolver
- TomasVotruba\UnusedPublic\CollectorMapper\MethodCallCollectorMapper
- TomasVotruba\UnusedPublic\NodeCollectorExtractor
# templates
- TomasVotruba\UnusedPublic\Templates\TemplateMethodCallsProvider
- TomasVotruba\UnusedPublic\Templates\TemplateRegexFinder
Expand Down Expand Up @@ -111,3 +109,4 @@ rules:
- TomasVotruba\UnusedPublic\Rules\UnusedPublicClassConstRule
- TomasVotruba\UnusedPublic\Rules\UnusedPublicPropertyRule
- TomasVotruba\UnusedPublic\Rules\LocalOnlyPublicClassMethodRule
- TomasVotruba\UnusedPublic\Rules\RelativeUnusedPublicClassMethodRule
9 changes: 3 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ parameters:
return_type: 99
param_type: 99
property_type: 99
# print_suggestions: true

cognitive_complexity:
function: 11

level: 8

paths:
- src
- tests

checkGenericClassInNonGenericObjectType: false

excludePaths:
- "*/Fixture/*"
- "*/Source/*"

ignoreErrors:
-
identifier: missingType.generics

# overly detailed
-
message: '#testRule\(\) has parameter (.*?) with no value type specified in iterable type array#'
Expand Down
4 changes: 2 additions & 2 deletions src/CallReferece/ParentCallReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use PHPStan\Reflection\ReflectionProvider;

final class ParentCallReferenceResolver
final readonly class ParentCallReferenceResolver
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private ReflectionProvider $reflectionProvider,
) {
}

Expand Down
7 changes: 4 additions & 3 deletions src/ClassTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ final class ClassTypeDetector
{
public function isTestClass(ClassReflection $classReflection): bool
{
return $classReflection->isSubclassOf('PHPUnit\Framework\TestCase') || $classReflection->isSubclassOf(
'PHPUnit_Framework_TestCase'
);
return $classReflection->isSubclassOf('PHPUnit\Framework\TestCase')
|| $classReflection->isSubclassOf('PHPUnit_Framework_TestCase')
|| $classReflection->implementsInterface('Behat\Behat\Context\Context')
;
}
}
6 changes: 3 additions & 3 deletions src/Collectors/Callable_/AttributeCallableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
use TomasVotruba\UnusedPublic\ValueObject\ClassAndMethodArrayExprs;

/**
* @implements Collector<AttributeGroup, array<string>|null>
* @implements Collector<AttributeGroup, non-empty-array<string>|null>
*/
final class AttributeCallableCollector implements Collector
final readonly class AttributeCallableCollector implements Collector
{
public function __construct(
private readonly Configuration $configuration,
private Configuration $configuration,
) {
}

Expand Down
8 changes: 4 additions & 4 deletions src/Collectors/Callable_/CallUserFuncCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<FuncCall, array<string>|null>
* @implements Collector<FuncCall, non-empty-array<string>|null>
*/
final class CallUserFuncCollector implements Collector
final readonly class CallUserFuncCollector implements Collector
{
public function __construct(
private readonly Configuration $configuration,
private readonly ClassTypeDetector $classTypeDetector,
private Configuration $configuration,
private ClassTypeDetector $classTypeDetector,
) {
}

Expand Down
14 changes: 7 additions & 7 deletions src/Collectors/ClassConstFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<ClassConstFetch, string[]>
* @implements Collector<ClassConstFetch, non-empty-array<string>|null>
*/
final class ClassConstFetchCollector implements Collector
final readonly class ClassConstFetchCollector implements Collector
{
public function __construct(
private readonly Configuration $configuration,
private readonly ClassTypeDetector $classTypeDetector,
private Configuration $configuration,
private ClassTypeDetector $classTypeDetector,
) {
}

Expand All @@ -32,12 +32,12 @@ public function getNodeType(): string

/**
* @param ClassConstFetch $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $this->configuration->isUnusedConstantsEnabled()) {
return [];
return null;
}

if (! $node->class instanceof Name) {
Expand All @@ -60,7 +60,7 @@ public function processNode(Node $node, Scope $scope): ?array
if ($classReflection->hasConstant($constantName)) {
$constantReflection = $classReflection->getConstant($constantName);
$declaringClass = $constantReflection->getDeclaringClass();
if ($declaringClass->getFileName() !== $classReflection->getFileName()) {
if ($declaringClass->getName() !== $classReflection->getName()) {
return [$declaringClass->getName() . '::' . $constantName];
}

Expand Down
8 changes: 4 additions & 4 deletions src/Collectors/FormTypeClassCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

/**
* Match Symfony data_class element in forms types, as those use magic setters/getters
* @implements Collector<ArrayItem, array<string>|null>
* @implements Collector<ArrayItem, non-empty-array<string>|null>
*/
final class FormTypeClassCollector implements Collector
final readonly class FormTypeClassCollector implements Collector
{
public function __construct(
private readonly Configuration $configuration,
private Configuration $configuration,
) {
}

Expand All @@ -30,7 +30,7 @@ public function getNodeType(): string

/**
* @param ArrayItem $node
* @return string[]|null
* @return non-empty-array<string>|null
*/
public function processNode(Node $node, Scope $scope): ?array
{
Expand Down
14 changes: 7 additions & 7 deletions src/Collectors/MethodCall/MethodCallCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<MethodCall, array<string>|null>
* @implements Collector<MethodCall, non-empty-array<string>|null>
*/
final class MethodCallCollector implements Collector
final readonly class MethodCallCollector implements Collector
{
public function __construct(
private readonly ParentCallReferenceResolver $parentCallReferenceResolver,
private readonly ClassMethodCallReferenceResolver $classMethodCallReferenceResolver,
private readonly Configuration $configuration,
private readonly ClassTypeDetector $classTypeDetector,
private readonly CallReferencesFlatter $callReferencesFlatter,
private ParentCallReferenceResolver $parentCallReferenceResolver,
private ClassMethodCallReferenceResolver $classMethodCallReferenceResolver,
private Configuration $configuration,
private ClassTypeDetector $classTypeDetector,
private CallReferencesFlatter $callReferencesFlatter,
) {
}

Expand Down
12 changes: 6 additions & 6 deletions src/Collectors/MethodCall/MethodCallableCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
use TomasVotruba\UnusedPublic\Configuration;

/**
* @implements Collector<MethodCallableNode, array<string>|null>
* @implements Collector<MethodCallableNode, non-empty-array<string>|null>
*/
final class MethodCallableCollector implements Collector
final readonly class MethodCallableCollector implements Collector
{
public function __construct(
private readonly ClassMethodCallReferenceResolver $classMethodCallReferenceResolver,
private readonly Configuration $configuration,
private readonly ClassTypeDetector $classTypeDetector,
private readonly CallReferencesFlatter $callReferencesFlatter,
private ClassMethodCallReferenceResolver $classMethodCallReferenceResolver,
private Configuration $configuration,
private ClassTypeDetector $classTypeDetector,
private CallReferencesFlatter $callReferencesFlatter,
) {
}

Expand Down
Loading

0 comments on commit d86448a

Please sign in to comment.