Skip to content
Merged
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
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,42 @@ concurrency:
cancel-in-progress: true

jobs:
psalm:
name: Static analysis
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: composer:v2
coverage: none

- name: Get Composer cache directories
id: composer-cache
run: |
echo "files_cache=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT"
echo "vcs_cache=$(composer config cache-vcs-dir)" >> "$GITHUB_OUTPUT"

- name: Retrieve cached packages
uses: actions/cache@v6
with:
path: |
${{ steps.composer-cache.outputs.files_cache }}
${{ steps.composer-cache.outputs.vcs_cache }}
key: ${{ runner.os }}-composer-${{ hashFiles('composer.json') }}
restore-keys: |
${{ runner.os }}-composer-

- name: Install dependencies
run: composer update --prefer-dist --prefer-stable --no-interaction --no-progress

- name: Run Psalm
run: vendor/bin/psalm --output-format=github --shepherd

php-cs-fixer:
name: Code style
runs-on: ubuntu-latest
Expand Down
17 changes: 12 additions & 5 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
/**
* @psalm-type Hook = array{hook_type: string, types: list<Union>, deprecated: bool, minimum_invoke_args: int<0, max>}
*/
class Plugin implements
final class Plugin implements
AfterEveryFunctionCallAnalysisInterface,
BeforeFileAnalysisInterface,
FunctionParamsProviderInterface,
Expand Down Expand Up @@ -81,6 +81,7 @@ class Plugin implements
*/
public static $parseErrors = [];

#[\Override]
public function __invoke(RegistrationInterface $registration, ?\SimpleXMLElement $config = null): void
{
$registration->registerHooksFromClass(static::class);
Expand Down Expand Up @@ -360,6 +361,7 @@ protected static function loadHooksFromFile(string $filepath): void
}
}

#[\Override]
public static function beforeAnalyzeFile(BeforeFileAnalysisEvent $event): void
{
$file_path = $event->getStatementsSource()->getFilePath();
Expand Down Expand Up @@ -594,6 +596,7 @@ public static function getDynamicHookData(string $hook_name, bool $is_action_not
return null;
}

#[\Override]
public static function afterEveryFunctionCallAnalysis(AfterEveryFunctionCallAnalysisEvent $event): void
{
$apply_filter_functions = [
Expand Down Expand Up @@ -732,6 +735,7 @@ public static function afterEveryFunctionCallAnalysis(AfterEveryFunctionCallAnal
*
* @psalm-pure
*/
#[\Override]
public static function getFunctionIds(): array
{
return [
Expand Down Expand Up @@ -760,6 +764,7 @@ public static function getFunctionIds(): array
/**
* @return ?array<int, Psalm\Storage\FunctionLikeParameter>
*/
#[\Override]
public static function getFunctionParams(FunctionParamsProviderEvent $event): ?array
{
$function_id = $event->getFunctionId();
Expand Down Expand Up @@ -1082,6 +1087,7 @@ public static function getFunctionParams(FunctionParamsProviderEvent $event): ?a
return $return;
}

#[\Override]
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union
{
if ($event->getFunctionId() === 'wp_parse_url') {
Expand Down Expand Up @@ -1304,7 +1310,7 @@ public static function registerHook(string $hook_name, array $types, string $hoo
}
}

class HookNodeVisitor extends PhpParser\NodeVisitorAbstract
final class HookNodeVisitor extends PhpParser\NodeVisitorAbstract
{
/** @var ?PhpParser\Comment\Doc */
protected $last_doc = null;
Expand Down Expand Up @@ -1332,6 +1338,7 @@ class HookNodeVisitor extends PhpParser\NodeVisitorAbstract
*/
protected $useStatementsNonClass = false;

#[\Override]
public function enterNode(PhpParser\Node $node)
{
$apply_filter_functions = [
Expand Down Expand Up @@ -1663,8 +1670,8 @@ public function enterNode(PhpParser\Node $node)
}
}

class HookNotFound extends PluginIssue {}
final class HookNotFound extends PluginIssue {}

class HookInvalidArgs extends PluginIssue {}
final class HookInvalidArgs extends PluginIssue {}

class DeprecatedHook extends PluginIssue {}
final class DeprecatedHook extends PluginIssue {}
154 changes: 154 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="7.3.0@6480ee65f252d9829b0c5a49d44c1fa263babfa2">
<file src="Plugin.php">
<ArgumentTypeCoercion>
<code><![CDATA[$statements_source]]></code>
</ArgumentTypeCoercion>
<InternalClass>
<code><![CDATA[ConstFetchAnalyzer::getConstType(
$statements_source,
$parser_param->name->toString(),
true,
$context,
)]]></code>
<code><![CDATA[ExpressionIdentifier::getExtendedVarId(
$parser_param,
null,
$statements_source,
)]]></code>
<code><![CDATA[ParseUrlReturnTypeProvider::getFunctionReturnType($event)]]></code>
<code><![CDATA[SimpleTypeInferer::infer(
$statements_source->getCodebase(),
$statements_source->node_data,
$parser_param,
$statements_source->getAliases(),
$statements_source,
)]]></code>
</InternalClass>
<InternalMethod>
<code><![CDATA[ConstFetchAnalyzer::getConstType(
$statements_source,
$parser_param->name->toString(),
true,
$context,
)]]></code>
<code><![CDATA[ExpressionIdentifier::getExtendedVarId(
$parser_param,
null,
$statements_source,
)]]></code>
<code><![CDATA[ParseUrlReturnTypeProvider::getFunctionReturnType($event)]]></code>
<code><![CDATA[SimpleTypeInferer::infer(
$statements_source->getCodebase(),
$statements_source->node_data,
$parser_param,
$statements_source->getAliases(),
$statements_source,
)]]></code>
</InternalMethod>
<InvalidPropertyAssignmentValue>
<code><![CDATA[$this->useStatements]]></code>
<code><![CDATA[$this->useStatements]]></code>
<code><![CDATA[$this->useStatements]]></code>
<code><![CDATA[$this->useStatements]]></code>
</InvalidPropertyAssignmentValue>
<InvalidReturnStatement>
<code><![CDATA[$arg->value]]></code>
</InvalidReturnStatement>
<InvalidReturnType>
<code><![CDATA[?string]]></code>
</InvalidReturnType>
<MissingParamType>
<code><![CDATA[$parser_param]]></code>
</MissingParamType>
<NoInterfaceProperties>
<code><![CDATA[$statements_source->node_data]]></code>
</NoInterfaceProperties>
<PossiblyFalseArgument>
<code><![CDATA[file_get_contents($filepath)]]></code>
</PossiblyFalseArgument>
<PossiblyFalseOperand>
<code><![CDATA[$base_dir]]></code>
<code><![CDATA[$base_dir]]></code>
<code><![CDATA[$cwd]]></code>
</PossiblyFalseOperand>
<PossiblyInvalidArgument>
<code><![CDATA[str_replace('{$abc}', '', $normalized_dynamic_hook_name)]]></code>
</PossiblyInvalidArgument>
<PossiblyInvalidOperand>
<code><![CDATA[$arg->name]]></code>
</PossiblyInvalidOperand>
<PossiblyNullArgument>
<code><![CDATA[$arg->dim]]></code>
<code><![CDATA[$code_location]]></code>
<code><![CDATA[$event->getContext()]]></code>
<code><![CDATA[$normalized_dynamic_hook_names]]></code>
<code><![CDATA[$normalized_hook_name]]></code>
</PossiblyNullArgument>
<PossiblyUndefinedVariable>
<code><![CDATA[$doc_block]]></code>
<code><![CDATA[$hook_type]]></code>
<code><![CDATA[$hook_type]]></code>
<code><![CDATA[$hook_type]]></code>
</PossiblyUndefinedVariable>
<RedundantCondition>
<code><![CDATA[isset($call_arg->name) && $call_arg->name instanceof PhpParser\Node\Identifier]]></code>
</RedundantCondition>
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $extended_var_id]]></code>
<code><![CDATA[! $found]]></code>
<code><![CDATA[! $hook_name]]></code>
<code><![CDATA[! $hook_name]]></code>
<code><![CDATA[! $hook_name]]></code>
<code><![CDATA[! $potential_hook_name]]></code>
<code><![CDATA[! $potential_hook_name]]></code>
<code><![CDATA[! $potential_hook_name]]></code>
<code><![CDATA[$potential_hook_name]]></code>
<code><![CDATA[$potential_hook_name]]></code>
<code><![CDATA[$potential_hook_name]]></code>
<code><![CDATA[$potential_hook_name]]></code>
<code><![CDATA[$potential_hook_name]]></code>
<code><![CDATA[$potential_hook_name]]></code>
<code><![CDATA[empty($config->hooks)]]></code>
<code><![CDATA[empty($hook['types'])]]></code>
<code><![CDATA[empty($node->alias->name)]]></code>
<code><![CDATA[empty($this->useStatements[ $class_name ])]]></code>
<code><![CDATA[empty($this->useStatements[ $node->alias->name ])]]></code>
<code><![CDATA[empty($use_object->alias->name)]]></code>
</RiskyTruthyFalsyComparison>
<UndefinedPropertyFetch>
<code><![CDATA[$call_arg->value->value]]></code>
<code><![CDATA[$node->args[ $hook_index + 1 ]->value]]></code>
<code><![CDATA[$node->args[ $hook_index + 1 ]->value]]></code>
<code><![CDATA[$node->args[ $hook_index + 1 ]->value]]></code>
<code><![CDATA[$node->args[ $hook_index + 1 ]->value]]></code>
<code><![CDATA[$node->args[ $hook_index + 1 ]->value]]></code>
<code><![CDATA[$node->args[ $hook_index + 1 ]->value]]></code>
<code><![CDATA[$node->args[ $hook_index ]->value]]></code>
<code><![CDATA[$node->args[ $hook_index ]->value]]></code>
<code><![CDATA[$node->args[0]->value]]></code>
<code><![CDATA[$node->args[0]->value]]></code>
<code><![CDATA[$node->args[0]->value]]></code>
<code><![CDATA[$node->args[0]->value]]></code>
</UndefinedPropertyFetch>
</file>
<file src="test.php">
<HookNotFound>
<code><![CDATA[add_filter('admin_notices', function () {
echo 'hi';
})]]></code>
<code><![CDATA[add_filter('foo', function (bool $post): void {
new WP_Post(new stdClass());
}, 10, 2)]]></code>
</HookNotFound>
<UnusedClosureParam>
<code><![CDATA[$post]]></code>
</UnusedClosureParam>
<UnusedVariable>
<code><![CDATA[$uploads]]></code>
<code><![CDATA[$url_host]]></code>
<code><![CDATA[$url_parts]]></code>
<code><![CDATA[$url_port]]></code>
</UnusedVariable>
</file>
</files>
10 changes: 10 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xmlns="https://getpsalm.org/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
findUnusedPsalmSuppress="true"
>
<projectFiles>
<file name="./test.php" />
Expand All @@ -11,6 +13,14 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<!-- The WordPress and WP-CLI stubs loaded by the plugin have docblocks Psalm can't parse -->
<InvalidDocblock>
<errorLevel type="suppress">
<directory name="vendor" />
</errorLevel>
</InvalidDocblock>
</issueHandlers>
<plugins>
<pluginClass class="PsalmWordPress\Plugin" />
<pluginClass class="Psalm\PhpUnitPlugin\Plugin" />
Expand Down
Loading