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
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ parameters:
paths:
- tests/PHPStan/

# TODO remove when minimum PHPStan is bumped to ^2
# PropertyProperty is the primary class in php-parser v4 (PHPStan 1.x) - no warning
# PropertyProperty is an alias to PropertyItem in php-parser v5 (PHPStan 2.x) - triggers these warnings
- message: '#Class PhpParser\\Node\\Stmt\\PropertyProperty not found\. It has been renamed to PhpParser\\Node\\PropertyItem#'
paths:
- src/PHPStan/Rules/PropertyNameIdToIDRule.php
- message: '#Fetching class constant class of deprecated class PhpParser\\Node\\Stmt\\PropertyProperty#'
paths:
- src/PHPStan/Rules/PropertyNameIdToIDRule.php

1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ rules:
# TODO gradually enable those rules
#- MLL\Utils\PHPStan\Rules\ThrowableClassNameRule
#- MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule
#- MLL\Utils\PHPStan\Rules\PropertyNameIdToIDRule
#- MLL\Utils\PHPStan\Rules\MissingClosureParameterTypehintRule
parameters:
# https://github.com/spaze/phpstan-disallowed-calls/blob/main/docs/custom-rules.md
Expand Down
31 changes: 31 additions & 0 deletions src/PHPStan/Rules/PropertyNameIdToIDRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace MLL\Utils\PHPStan\Rules;

use PhpParser\Node;
use PhpParser\Node\Stmt\PropertyProperty;

final class PropertyNameIdToIDRule extends IdToIDRule
{
public function getNodeType(): string
{
return PropertyProperty::class;
}

protected function getErrorIdentifier(): string
{
return 'mll.propertyNameIdToID';
}

protected function formatNameForMessage(string $name): string
{
return '$' . $name;
}

protected function extractName(Node $node): string
{
assert($node instanceof PropertyProperty);

return $node->name->name;
}
}
7 changes: 5 additions & 2 deletions tests/PHPStan/CapitalizationOfIDRuleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ public static function dataIntegrationTests(): iterable
'Name of Stmt_Class "LabIdProcessor" should use "ID" instead of "Id", rename it to "LabIDProcessor".',
],
7 => [
'Name of Stmt_PropertyProperty "$patientId" should use "ID" instead of "Id", rename it to "$patientID".',
],
9 => [
'Name of Stmt_ClassMethod "getLabId" should use "ID" instead of "Id", rename it to "getLabID".',
],
12 => [
14 => [
'Name of Stmt_ClassMethod "processLabId" should use "ID" instead of "Id", rename it to "processLabID".',
'Name of Param "$labId" should use "ID" instead of "Id", rename it to "$labID".',
],
14 => [
16 => [
'Name of Expr_Variable "$sampleId" should use "ID" instead of "Id", rename it to "$sampleID".',
'Name of Expr_Variable "$labId" should use "ID" instead of "Id", rename it to "$labID".',
],
Expand Down
4 changes: 3 additions & 1 deletion tests/PHPStan/data/correct-capitalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

class CorrectCapitalization
{
private int $patientID = 1;

public function getLabID(): int
{
return 1;
return $this->patientID;
}

public function processLabID(int $labID): void
Expand Down
4 changes: 3 additions & 1 deletion tests/PHPStan/data/wrong-capitalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

class LabIdProcessor
{
private int $patientId = 1;

public function getLabId(): int
{
return 1;
return $this->patientId;
}

public function processLabId(int $labId): void
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/phpstan-test.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ rules:
- MLL\Utils\PHPStan\Rules\ParameterNameIdToIDRule
- MLL\Utils\PHPStan\Rules\MethodNameIdToIDRule
- MLL\Utils\PHPStan\Rules\ClassNameIdToIDRule
- MLL\Utils\PHPStan\Rules\PropertyNameIdToIDRule