-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use TomasVotruba/unused-public (#264)
- Loading branch information
Showing
33 changed files
with
1,650 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,9 @@ | ||
# these rule focus on the whole-project analysis, see https://phpstan.org/developing-extensions/collectors | ||
rules: | ||
- Symplify\PHPStanRules\Rules\DeadCode\UnusedPublicClassConstRule | ||
- Symplify\PHPStanRules\Rules\DeadCode\UnusedPublicClassMethodRule | ||
|
||
services: | ||
# for UnusedPublicClassConstRule | ||
- | ||
class: Symplify\PHPStanRules\Collector\ClassConst\ClassConstFetchCollector | ||
tags: [phpstan.collector] | ||
|
||
- | ||
class: Symplify\PHPStanRules\Collector\ClassConst\PublicClassLikeConstCollector | ||
tags: [phpstan.collector] | ||
|
||
# for UnusedPublicClassMethodRule | ||
- | ||
class: Symplify\PHPStanRules\Collector\ClassMethod\PublicClassMethodCollector | ||
tags: [phpstan.collector] | ||
|
||
- | ||
class: Symplify\PHPStanRules\Collector\ClassMethod\MethodCallCollector | ||
tags: [phpstan.collector] | ||
includes: | ||
- ../vendor/tomasvotruba/unused-public/config/extension.neon | ||
|
||
parameters: | ||
unused_public: | ||
methods: true | ||
properties: true | ||
constants: true | ||
static_properties: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = space | ||
indent_size = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
composer.lock | ||
|
||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Find Unused Public Constants, Properties and Methods in Your Code | ||
|
||
It's easy to find unused private class elements, because they're not used in the class itself: | ||
|
||
```diff | ||
final class Book | ||
{ | ||
public function getTitle(): string | ||
{ | ||
// ... | ||
} | ||
|
||
- private function getSubtitle(): string | ||
- { | ||
- // ... | ||
- } | ||
} | ||
``` | ||
|
||
But what about public class elements? | ||
|
||
<br> | ||
|
||
**How can we detect such element?** | ||
|
||
* find a e.g. public method | ||
* find all public method calls | ||
* compare those in simple diff | ||
* if the public method is not found, it probably unused | ||
|
||
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. | ||
|
||
<br> | ||
|
||
## Install | ||
|
||
```bash | ||
composer require tomasvotruba/unused-public --dev | ||
``` | ||
|
||
The package is available on PHP 7.2-8.1 versions in tagged releases. | ||
|
||
<br> | ||
|
||
## Usage | ||
|
||
With PHPStan extension installer, everything is ready to run. | ||
|
||
Enable each item on their own with simple configuration: | ||
|
||
```neon | ||
# phpstan.neon | ||
parameters: | ||
unused_public: | ||
methods: true | ||
properties: true | ||
constants: true | ||
static_properties: true | ||
``` | ||
|
||
<br> | ||
|
||
## Known Limitations | ||
|
||
In some cases, the method reports false positives: | ||
|
||
* it's not possible to detect unused public method that are called only in Twig templates | ||
* following cases are skipped | ||
* public function in Twig extensions - those are functions/filters callable | ||
|
||
<br> | ||
|
||
## Skip False Positives | ||
|
||
Is element reported as unused, but it's actually used? | ||
|
||
Mark the class or element wit `@api` to declare it as public API and skip it: | ||
|
||
```php | ||
final class Book | ||
{ | ||
/** | ||
* @api | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "tomasvotruba/unused-public", | ||
"type": "phpstan-extension", | ||
"description": "Detect unused public properties, constants and methods in your code", | ||
"license": "MIT", | ||
"require": { | ||
"php": "^7.2|8.0", | ||
"phpstan/phpstan": "^1.9.3", | ||
"nette/utils": "^3.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"TomasVotruba\\UnusedPublic\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"phpstan": { | ||
"includes": [ | ||
"config/extension.neon" | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.