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
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.github export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
/ecs.php export-ignore
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^12.0"
"phpunit/phpunit": "^12.0",
"symplify/easy-coding-standard": "^12.5"
}
}
63 changes: 62 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/Rule/CommandClassShouldBeHelpCommandHandlerClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
final class CommandClassShouldBeHelpCommandHandlerClass implements Rule
{

public function __construct(
private readonly PhpDocParser $parser,
private readonly Lexer $phpDocLexer,
Expand All @@ -44,7 +43,7 @@ public function processNode(Node $node, Scope $scope): array
$className = $node->name->name;
}

if (!str_ends_with($className, 'Command')) {
if (! str_ends_with($className, 'Command')) {
return [];
}

Expand All @@ -70,7 +69,7 @@ public function processNode(Node $node, Scope $scope): array
if ($tag->value instanceof GenericTagValueNode) {
$find = true;
$value = $tag->value->value;
if (!str_ends_with($value, 'CommandHandler')) {
if (! str_ends_with($value, 'CommandHandler')) {
return [
RuleErrorBuilder::message(
sprintf(
Expand All @@ -92,5 +91,4 @@ public function processNode(Node $node, Scope $scope): array

return [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
final class EventListenerClassShouldBeIncludeAsListenerAttribute implements Rule
{

public function __construct(private readonly ReflectionProvider $reflectionProvider)
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider
) {
}

public function getNodeType(): string
Expand All @@ -39,7 +39,7 @@ public function processNode(Node $node, Scope $scope): array
$className = $node->name->name;
}

if (!str_ends_with($className, 'EventListener')) {
if (! str_ends_with($className, 'EventListener')) {
return [];
}

Expand All @@ -61,8 +61,9 @@ public function processNode(Node $node, Scope $scope): array

if ($find === false) {
return [
RuleErrorBuilder::message('Event listener class should be include attribute #[AsEventListener]')->build(
),
RuleErrorBuilder::message(
'Event listener class should be include attribute #[AsEventListener]'
)->build(),
];
}

Expand Down
17 changes: 7 additions & 10 deletions src/Rule/NotShouldPhpdocReturnIfExistTypeHint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;

/**
* @implements Rule<Class_>
*/
final class NotShouldPhpdocReturnIfExistTypeHint implements Rule
{

public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly PhpDocParser $parser,
Expand All @@ -33,9 +31,6 @@ public function getNodeType(): string
return Class_::class;
}

/**
* @throws ShouldNotHappenException
*/
public function processNode(Node $node, Scope $scope): array
{
$fullyQualifiedClassName = $node->namespacedName?->toString();
Expand All @@ -54,14 +49,13 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$doc = (string)$method->getDocComment();
$doc = (string) $method->getDocComment();
if ($doc === '') {
continue;
}


$returnType = $method->getReturnType();
if ($returnType === null || !method_exists($returnType, 'getName')) {
if ($returnType === null || ! method_exists($returnType, 'getName')) {
return [];
}

Expand All @@ -76,10 +70,13 @@ public function processNode(Node $node, Scope $scope): array
if ($tag->value instanceof ReturnTagValueNode) {
$value = $tag->value->type->name;
if ($value === $returnTypeName
&& $reflection->getName() === $method->getBetterReflection()->getLocatedSource()->getName()) {
&& $reflection->getName() === $method->getBetterReflection()
->getLocatedSource()
->getName()) {
$errors[] = \PHPStan\Rules\RuleErrorBuilder::message(
'PhpDoc attribute @return for method ' . $method->getName() . ' can be remove'
)->line((int)$method->getStartLine())->build();
)->line((int) $method->getStartLine())
->build();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixture/EventListener/AsEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[Attribute]
class AsEventListener
{
public function __construct(
)
{}
public function __construct()
{
}
}
6 changes: 2 additions & 4 deletions tests/Fixture/EventListener/TestClassEventListener.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

declare(strict_types=1);
namespace Simtel\PHPStanRules\Tests\Fixture\EventListener;

use Simtel\PHPStanRules\Tests\Fixture\EventListener\AsEventListener;
namespace Simtel\PHPStanRules\Tests\Fixture\EventListener;

#[AsEventListener]
class TestClassEventListener
{
public function method():void
public function method(): void
{

}
}
15 changes: 4 additions & 11 deletions tests/Rules/CommandClassShouldBeHelpCommandHandlerClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

namespace Simtel\PHPStanRules\Tests\Rules;

use Simtel\PHPStanRules\Rule\CommandClassShouldBeHelpCommandHandlerClass;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPStan\PhpDocParser\ParserConfig;
use Simtel\PHPStanRules\Rule\CommandClassShouldBeHelpCommandHandlerClass;

class CommandClassShouldBeHelpCommandHandlerClassTest extends RuleTestCase
{

/**
* @inheritDoc
*/
Expand All @@ -42,20 +41,14 @@ public function testCorrectSeeAttribute(): void
public function testExistsSeeAttribute(): void
{
$this->analyse([__DIR__ . '/../data/command_handler_data2.php'], [
[
'PhpDoc command class should be include @see attribute with CommandHandler class name',
10
]
['PhpDoc command class should be include @see attribute with CommandHandler class name', 10]
]);
}

public function testExistsPhpDoc(): void
{
$this->analyse([__DIR__ . '/../data/command_handler_data3.php'], [
[
'Command class should be include phpDoc with @see attribute',
7
]
['Command class should be include phpDoc with @see attribute', 7]
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class EventListenerClassShouldBeIncludeAsListenerAttributeTest extends RuleTestCase
{

/**
* @inheritDoc
*/
Expand All @@ -27,11 +26,7 @@ public function testExistsNeedAttribute(): void
public function testExistsAttribute(): void
{
$this->analyse([__DIR__ . '/../Fixture/EventListener/TestNotCorrectClassEventListener.php'], [
[
'Event listener class should be include attribute #[AsEventListener]',
7
]
['Event listener class should be include attribute #[AsEventListener]', 7]
]);
}

}
11 changes: 2 additions & 9 deletions tests/Rules/NotShouldPhpdocReturnIfExistTypeHintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

class NotShouldPhpdocReturnIfExistTypeHintTest extends RuleTestCase
{

/**
* @inheritDoc
*/
Expand All @@ -33,14 +32,8 @@ public function getRule(): Rule
public function testWithError(): void
{
$this->analyse([__DIR__ . '/../Fixture/Return/MethodsWithTypeHintAndReturn.php'], [
[
'PhpDoc attribute @return for method someMethod can be remove',
12
],
[
'PhpDoc attribute @return for method getInt can be remove',
20
],
['PhpDoc attribute @return for method someMethod can be remove', 12],
['PhpDoc attribute @return for method getInt can be remove', 20],
]);
}
}
1 change: 0 additions & 1 deletion tests/data/command_handler_data1.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
*/
class TestClassCommand
{

}
1 change: 0 additions & 1 deletion tests/data/command_handler_data2.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
*/
class TestClassCommand
{

}
1 change: 0 additions & 1 deletion tests/data/command_handler_data3.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class TestClassCommand
{

}