Skip to content

Commit

Permalink
Report unused baseline entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-worman committed Jan 18, 2023
1 parent b6bdc94 commit cadea48
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 6 deletions.
3 changes: 3 additions & 0 deletions config.xsd
Expand Up @@ -44,6 +44,8 @@
<xs:attribute name="findUnusedCode" type="xs:boolean" default="false" />
<xs:attribute name="findUnusedVariablesAndParams" type="xs:boolean" default="false" />
<xs:attribute name="findUnusedPsalmSuppress" type="xs:boolean" default="false" />
<!-- TODO: Update default to true in Psalm 6 -->
<xs:attribute name="findUnusedBaselineEntry" type="xs:boolean" default="false" />
<xs:attribute name="hideExternalErrors" type="xs:boolean" default="false" />
<xs:attribute name="hoistConstants" type="xs:boolean" default="false" />
<xs:attribute name="ignoreInternalFunctionFalseReturn" type="xs:boolean" default="true" />
Expand Down Expand Up @@ -476,6 +478,7 @@
<xs:element name="UnsafeGenericInstantiation" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UnsafeInstantiation" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UnsupportedReferenceUsage" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UnusedBaselineEntry" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UnusedClass" type="ClassIssueHandlerType" minOccurs="0" />
<xs:element name="UnusedClosureParam" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UnusedConstructor" type="MethodIssueHandlerType" minOccurs="0" />
Expand Down
4 changes: 4 additions & 0 deletions docs/running_psalm/configuration.md
Expand Up @@ -491,6 +491,10 @@ class PremiumCar extends StandardCar {
```
`ImplementedReturnTypeMismatch - The inherited return type 'list{'motor', 'brakes', 'wheels'}' for StandardCar::getSystems is different to the implemented return type for PremiumCar::getsystems 'list{'motor', 'brakes', 'wheels', 'rear parking sensor'}'`

#### findUnusedBaselineEntry

Emits [UnusedBaselineEntry](issues/UnusedBaselineEntry.md) when a baseline entry
is not being used to suppress an issue.

## Project settings

Expand Down
1 change: 1 addition & 0 deletions docs/running_psalm/issues.md
Expand Up @@ -282,6 +282,7 @@
- [UnsafeGenericInstantiation](issues/UnsafeGenericInstantiation.md)
- [UnsafeInstantiation](issues/UnsafeInstantiation.md)
- [UnsupportedReferenceUsage](issues/UnsupportedReferenceUsage.md)
- [UnusedBaselineEntry](issues/UnusedBaselineEntry.md)
- [UnusedClass](issues/UnusedClass.md)
- [UnusedClosureParam](issues/UnusedClosureParam.md)
- [UnusedConstructor](issues/UnusedConstructor.md)
Expand Down
5 changes: 5 additions & 0 deletions docs/running_psalm/issues/UnusedBaselineEntry.md
@@ -0,0 +1,5 @@
# UnusedBaselineEntry

Emitted when a baseline entry is not being used to suppress an issue.

Enabled by [findUnusedBaselineEntry](../configuration.md#findunusedbaselineentry)
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Expand Up @@ -701,11 +701,6 @@
<code>hasLowercaseString</code>
</PossiblyUnusedMethod>
</file>
<file src="tests/Internal/Codebase/InternalCallMapHandlerTest.php">
<UnusedPsalmSuppress>
<code>UndefinedMethod</code>
</UnusedPsalmSuppress>
</file>
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrowFunction.php">
<PossiblyUndefinedStringArrayOffset>
<code>$subNodes['expr']</code>
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Expand Up @@ -12,6 +12,7 @@
limitMethodComplexity="true"
errorBaseline="psalm-baseline.xml"
findUnusedPsalmSuppress="true"
findUnusedBaselineEntry="true"
>
<stubs>
<file name="stubs/phpparser.phpstub"/>
Expand Down
10 changes: 10 additions & 0 deletions src/Psalm/Config.php
Expand Up @@ -460,6 +460,11 @@ class Config
*/
public $find_unused_psalm_suppress = false;

/**
* TODO: Psalm 6: Update default to be true and remove warning.
*/
public bool $find_unused_baseline_entry = false;

/**
* @var bool
*/
Expand Down Expand Up @@ -1061,6 +1066,7 @@ private static function fromXmlAndPaths(
'allowInternalNamedArgumentsCalls' => 'allow_internal_named_arg_calls',
'allowNamedArgumentCalls' => 'allow_named_arg_calls',
'findUnusedPsalmSuppress' => 'find_unused_psalm_suppress',
'findUnusedBaselineEntry' => 'find_unused_baseline_entry',
'reportInfo' => 'report_info',
'restrictReturnTypes' => 'restrict_return_types',
'limitMethodComplexity' => 'limit_method_complexity',
Expand Down Expand Up @@ -1164,6 +1170,10 @@ private static function fromXmlAndPaths(
$config->use_igbinary = version_compare($igbinary_version, '2.0.5') >= 0;
}

if (!isset($config_xml['findUnusedBaselineEntry']) && !defined('__IS_TEST_ENV__')) {
fwrite(STDERR, 'Warning: "findUnusedBaselineEntry" will be defaulted to "true" in Psalm 6. You should'
. ' explicitly enable or disable this setting.' . PHP_EOL);
}

if (isset($config_xml['findUnusedCode'])) {
$attribute_text = (string) $config_xml['findUnusedCode'];
Expand Down
11 changes: 11 additions & 0 deletions src/Psalm/Issue/UnusedBaselineEntry.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Psalm\Issue;

class UnusedBaselineEntry extends ClassIssue
{
public const ERROR_LEVEL = -1;
public const SHORTCODE = 316;
}
34 changes: 34 additions & 0 deletions src/Psalm/IssueBuffer.php
Expand Up @@ -14,6 +14,7 @@
use Psalm\Issue\ConfigIssue;
use Psalm\Issue\MixedIssue;
use Psalm\Issue\TaintedInput;
use Psalm\Issue\UnusedBaselineEntry;
use Psalm\Issue\UnusedPsalmSuppress;
use Psalm\Plugin\EventHandler\Event\AfterAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\BeforeAddIssueEvent;
Expand Down Expand Up @@ -611,6 +612,39 @@ public static function finish(
$issues_data[$file_path][$key] = $issue_data;
}
}

if ($codebase->config->find_unused_baseline_entry) {
foreach ($issue_baseline as $file_path => $issues) {
foreach ($issues as $issue_name => $issue) {
if ($issue['o'] !== 0) {
$issues_data[$file_path][] = new IssueData(
Config::REPORT_ERROR,
0,
0,
UnusedBaselineEntry::getIssueType(),
sprintf(
'Baseline for issue "%s" has %d extra %s.',
$issue_name,
$issue['o'],
$issue['o'] === 1 ? 'entry' : 'entries',
),
$file_path,
'',
'',
'',
0,
0,
0,
0,
0,
0,
UnusedBaselineEntry::SHORTCODE,
UnusedBaselineEntry::ERROR_LEVEL,
);
}
}
}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/Internal/Codebase/InternalCallMapHandlerTest.php
Expand Up @@ -1395,7 +1395,6 @@ private function assertParameter(array $normalizedEntry, ReflectionParameter $pa
}
}

/** @psalm-suppress UndefinedMethod */
public function assertEntryReturnType(ReflectionFunctionAbstract $function, string $entryReturnType): void
{
if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
Expand Down

0 comments on commit cadea48

Please sign in to comment.