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

Unused function, extension and package detection #11365

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions bin/stubs/gen_preloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
RecursiveIteratorIterator::LEAVES_ONLY,
) as $f
) {
$f = str_replace(['/', '.php', 'src\\'], ['\\', '', ''], $f);
$classes[$f] = true;
if (str_ends_with($f, '.php')) {
$f = str_replace(['/', '.php', 'src\\'], ['\\', '', ''], $f);
$classes[$f] = true;
}
}

foreach ($classes as $class => $_) {
Expand Down
19 changes: 19 additions & 0 deletions config.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<xs:element name="universalObjectCrates" type="UniversalObjectCratesType" minOccurs="0" maxOccurs="1" />
<xs:element name="enableExtensions" type="ExtensionsType" minOccurs="0" maxOccurs="1" />
<xs:element name="disableExtensions" type="ExtensionsType" minOccurs="0" maxOccurs="1" />
<xs:element name="ignoreUnusedExtensions" type="AnyExtensionsType" minOccurs="0" maxOccurs="1" />
<xs:element name="ignoreUnusedComposerPackages" type="PackagesType" minOccurs="0" maxOccurs="1" />
</xs:choice>

<xs:attribute name="autoloader" type="xs:string" />
Expand Down Expand Up @@ -114,6 +116,7 @@
<xs:complexType name="StubsAttributeType">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="preloadClasses" type="xs:boolean" default="false" />
<xs:attribute name="extension" type="xs:string" default="" />
<xs:anyAttribute processContents="skip" />
</xs:complexType>

Expand Down Expand Up @@ -748,6 +751,18 @@
</xs:sequence>
</xs:complexType>

<xs:complexType name="AnyExtensionsType">
<xs:sequence>
<xs:element name="extension" maxOccurs="unbounded" type="PackageAttributeType" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="PackagesType">
<xs:sequence>
<xs:element name="package" maxOccurs="unbounded" type="PackageAttributeType" />
</xs:sequence>
</xs:complexType>

<xs:simpleType name="ExtensionType">
<xs:restriction base="xs:string">
<xs:enumeration value="amqp"/>
Expand All @@ -774,4 +789,8 @@
<xs:complexType name="ExtensionAttributeType">
<xs:attribute name="name" type="ExtensionType" use="required" />
</xs:complexType>

<xs:complexType name="PackageAttributeType">
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
31 changes: 30 additions & 1 deletion docs/running_psalm/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,31 @@ Optional. A list of extensions to disable. By default, only extensions required
</disableExtensions>
```


#### &lt;ignoreUnusedComposerPackages&gt;

Optional. A list of packages for which [UnusedComposerPackage](issues/UnusedComposerPackage.md) issues should not be emitted, even if they're not used in the current project.

Useful for example for peer dependencies (dependencies which are only used by another dependency, aren't explicitly required by that dependency's composer.json, and aren't used in the current project).

```xml
<ignoreUnusedComposerPackages>
<package name="symfony/polyfill-php84"/>
</ignoreUnusedComposerPackages>
```

#### &lt;ignoreUnusedExtensions&gt;

Optional. A list of extensions for which [UnusedExtension](issues/UnusedExtension.md) issues should not be emitted, even if they're not used in the current project.

Useful for example for peer dependencies (extensions which are only used by another dependency, aren't explicitly required by that dependency's composer.json, and aren't used in the current project).

```xml
<ignoreUnusedExtensions>
<extension name="bcmath"/>
</ignoreUnusedExtensions>
```

#### &lt;plugins&gt;
Optional. A list of `<plugin filename="path_to_plugin.php" />` entries. See the [Plugins](plugins/using_plugins.md) section for more information.

Expand All @@ -645,12 +670,16 @@ functions without the implementations.

You can find a list of stubs for common classes [here](https://github.com/JetBrains/phpstorm-stubs).
List out each file with `<file name="path/to/file.php" />`. In case classes to be tested use parent classes
or interfaces defined in a stub file, this stub should be configured with attribute `preloadClasses="true"`.
or interfaces defined in a stub file, this stub should be configured with attribute `preloadClasses="true"`.

Extension stubs must also specify the name of the extension (used for dead code detection).

```xml
<stubs>
<file name="path/to/file.php" />
<file name="path/to/abstract-class.php" preloadClasses="true" />
<file name="path/to/extensionStub.php" extension="extension-name" />
<file name="path/to/extensionStub2.php" preloadClasses="true" extension="extension-name" />
</stubs>
```

Expand Down
3 changes: 3 additions & 0 deletions docs/running_psalm/error_levels.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,12 @@ These issues are treated as errors at level 7 and below.
- [UnnecessaryVarAnnotation](issues/UnnecessaryVarAnnotation.md)
- [UnusedClass](issues/UnusedClass.md)
- [UnusedClosureParam](issues/UnusedClosureParam.md)
- [UnusedComposerPackage](issues/UnusedComposerPackage.md)
- [UnusedConstructor](issues/UnusedConstructor.md)
- [UnusedDocblockParam](issues/UnusedDocblockParam.md)
- [UnusedExtension](issues/UnusedExtension.md)
- [UnusedForeachValue](issues/UnusedForeachValue.md)
- [UnusedFunction](issues/UnusedFunction.md)
- [UnusedMethod](issues/UnusedMethod.md)
- [UnusedParam](issues/UnusedParam.md)
- [UnusedProperty](issues/UnusedProperty.md)
Expand Down
2 changes: 2 additions & 0 deletions docs/running_psalm/issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,10 @@
- [UnusedBaselineEntry](issues/UnusedBaselineEntry.md)
- [UnusedClass](issues/UnusedClass.md)
- [UnusedClosureParam](issues/UnusedClosureParam.md)
- [UnusedComposerPackage](issues/UnusedComposerPackage.md)
- [UnusedConstructor](issues/UnusedConstructor.md)
- [UnusedDocblockParam](issues/UnusedDocblockParam.md)
- [UnusedExtension](issues/UnusedExtension.md)
- [UnusedForeachValue](issues/UnusedForeachValue.md)
- [UnusedFunctionCall](issues/UnusedFunctionCall.md)
- [UnusedIssueHandlerSuppression](issues/UnusedIssueHandlerSuppression.md)
Expand Down
7 changes: 7 additions & 0 deletions docs/running_psalm/issues/UnusedComposerPackage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# UnusedComposerPackage

Emitted when `composer.json` contains a package that is not referenced in the analyzed project.

To fix, remove the package from the `require` section of `composer.json`.

Peer dependencies (dependencies which are only used by another dependency, aren't explicitly required by that dependency's composer.json, and aren't used in the current project) may be excluded from unused composer package detection by using the [ignoreUnusedComposerPackages](https://psalm.dev/docs/running_psalm/configuration/#ignoreunusedcomposerpackages) config.
7 changes: 7 additions & 0 deletions docs/running_psalm/issues/UnusedExtension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# UnusedExtension

Emitted when `composer.json` contains an extension that is not referenced in the analyzed project.

To fix, remove that extension from the `require` section of `composer.json`.

Peer dependencies (extensions which are only used by another dependency, aren't explicitly required by that dependency's composer.json, and aren't used in the current project) may be excluded from unused composer package detection by using the [ignoreUnusedExtensions](https://psalm.dev/docs/running_psalm/configuration/#ignoreunusedextensions) config.
14 changes: 14 additions & 0 deletions docs/running_psalm/issues/UnusedFunction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# UnusedFunction

Emitted when `--find-dead-code` is turned on and Psalm cannot find any uses of a
given function.

If this class is used and part of the public API, annotate it with `@psalm-api`.

```php
<?php

function a() {}
function b() {}
$a = a();
```
34 changes: 34 additions & 0 deletions src/Psalm/CodeLocation/ComposerJsonLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Psalm\CodeLocation;

use Psalm\CodeLocation;

/** @psalm-immutable */
final class ComposerJsonLocation extends CodeLocation
{
public function __construct(
string $file_path,
int $file_start,
int $file_end,
int $line_number,
) {
$this->file_start = $file_start;
// matches how CodeLocation works
$this->file_end = $file_end - 1;

$this->raw_file_start = $file_start;
$this->raw_file_end = $file_end;
$this->raw_line_number = $line_number;

$this->file_path = $file_path;
$this->file_name = 'composer.json';
$this->single_line = false;

$this->preview_start = $this->file_start;

$this->docblock_line_number = $line_number;
}
}
Loading
Loading