Skip to content

Commit ff113a9

Browse files
committed
Merge branch 'master' of github.com:FiveLab/CiRules
2 parents 22a8b01 + e8ae364 commit ff113a9

File tree

49 files changed

+1478
-154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1478
-154
lines changed

config/phpstan.services.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ services:
2323
class: \FiveLab\Component\CiRules\PhpStan\ForbiddenPassArgumentAsReferenceRule
2424
arguments: [ PhpParser\Node\Stmt\ClassMethod ]
2525
tags: [ phpstan.rules.rule ]
26+
27+
-
28+
class: \FiveLab\Component\CiRules\PhpStan\MethodCallConsistencyRule
29+
arguments: [ @reflectionProvider ]
30+
tags: [ phpstan.rules.rule ]
31+
32+
-
33+
class: \FiveLab\Component\CiRules\PhpStan\FunctionStrictModeRule
34+
tags: [ phpstan.rules.rule ]

src/PhpCs/FiveLab/ErrorCodes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ final class ErrorCodes
3030
public const LINE_AFTER_NOT_ALLOWED = 'LineAfterNotAllowed';
3131
public const LINE_BEFORE_NOT_ALLOWED = 'LineBeforeNotAllowed';
3232
public const MISSED_CONSTANT_TYPE = 'MissedConstantType';
33+
public const NAMESPACE_WRONG = 'NamespaceWrong';
34+
public const ARRAYS_DOC_VECTOR = 'ArraysDocVector';
35+
public const PHPDOC_NOT_ALLOWED = 'PhpDocNotAllowed';
3336
}

src/PhpCs/FiveLab/Sniffs/AbstractFunctionDocCommentSniff.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use PHP_CodeSniffer\Files\File;
1717
use PHP_CodeSniffer\Sniffs\Sniff;
1818

19-
/**
20-
* Abstract sniff for check doc comments.
21-
*/
2219
abstract class AbstractFunctionDocCommentSniff implements Sniff
2320
{
2421
public function register(): array
@@ -73,6 +70,7 @@ final public function process(File $phpcsFile, mixed $stackPtr): void
7370

7471
$commentLines = \explode($phpcsFile->eolChar, $commentContent); // @phpstan-ignore-line
7572
$commentLines = \array_map('\trim', $commentLines);
73+
$countCommentLines = \count($commentLines);
7674

7775
if ($commentLines[0] === '/**') {
7876
$startCommentLine++;
@@ -92,7 +90,7 @@ final public function process(File $phpcsFile, mixed $stackPtr): void
9290
return $line;
9391
}, $commentLines);
9492

95-
$this->processLines($phpcsFile, $startCommentLine, $commentLines, $functionNameToken['content']);
93+
$this->processLines($phpcsFile, $startCommentLine, $commentLines, $functionNameToken['content'], $countCommentLines);
9694
}
9795

9896
/**
@@ -102,6 +100,7 @@ final public function process(File $phpcsFile, mixed $stackPtr): void
102100
* @param int $startLineNumber
103101
* @param array<string> $lines
104102
* @param string $functionName
103+
* @param int $countCommentLines
105104
*/
106-
abstract protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName): void;
105+
abstract protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName, int $countCommentLines): void;
107106
}

src/PhpCs/FiveLab/Sniffs/Commenting/AnnotationsSniff.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
use FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\AbstractFunctionDocCommentSniff;
1818
use PHP_CodeSniffer\Files\File;
1919

20-
/**
21-
* Check annotations in comments.
22-
*/
2320
class AnnotationsSniff extends AbstractFunctionDocCommentSniff
2421
{
25-
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName): void
22+
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName, int $countCommentLines): void
2623
{
2724
foreach ($lines as $lineNumber => $line) {
2825
if ($line && '@' === $line[0]) {
@@ -37,14 +34,6 @@ protected function processLines(File $phpcsFile, int $startLineNumber, array $li
3734
}
3835
}
3936

40-
/**
41-
* Process annotation
42-
*
43-
* @param File $phpcsFile
44-
* @param int $lineNumber
45-
* @param string $annotation
46-
* @param string $value
47-
*/
4837
private function processAnnotation(File $phpcsFile, int $lineNumber, string $annotation, string $value): void
4938
{
5039
if ('return' === $annotation && 'void' === $value) {
@@ -68,5 +57,13 @@ private function processAnnotation(File $phpcsFile, int $lineNumber, string $ann
6857
);
6958
}
7059
}
60+
61+
if (\str_contains($value, '[]')) {
62+
$phpcsFile->addErrorOnLine(
63+
'Please use vector type annotation for arrays.',
64+
$lineNumber,
65+
ErrorCodes::ARRAYS_DOC_VECTOR
66+
);
67+
}
7168
}
7269
}

src/PhpCs/FiveLab/Sniffs/Commenting/ClassPropertyDocSniff.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ public function process(File $phpcsFile, mixed $stackPtr): void
3434
$tokens = $phpcsFile->getTokens();
3535
$token = $tokens[$stackPtr];
3636

37-
$functionPtr = $phpcsFile->findNext([T_FUNCTION], $token['comment_closer'] + 1, null, false, null, true);
38-
$openParenthesisPtr = $phpcsFile->findNext([T_OPEN_PARENTHESIS], $token['comment_closer'] + 1, null, false, null, true);
37+
$functionPtr = $phpcsFile->findNext([T_FUNCTION], $token['comment_closer'] + 1, local: true);
38+
$openParenthesisPtr = $phpcsFile->findNext([T_OPEN_PARENTHESIS], $token['comment_closer'] + 1, local: true);
3939

4040
if ($functionPtr || $openParenthesisPtr) {
4141
// This is a function.
4242
return;
4343
}
4444

45-
$visibilityPtr = $phpcsFile->findNext([T_PRIVATE, T_PROTECTED, T_PUBLIC], $token['comment_closer'] + 1, null, false, null, true);
46-
$varPtr = $phpcsFile->findNext([T_VARIABLE], $token['comment_closer'] + 1, null, false, null, true);
45+
$visibilityPtr = $phpcsFile->findNext([T_PRIVATE, T_PROTECTED, T_PUBLIC], $token['comment_closer'] + 1, local: true);
46+
$varPtr = $phpcsFile->findNext([T_VARIABLE], $token['comment_closer'] + 1, local: true);
4747

4848
if (!$visibilityPtr || !$varPtr) {
4949
// After close comment we must have visibility scope and variable.
@@ -54,12 +54,22 @@ public function process(File $phpcsFile, mixed $stackPtr): void
5454
$commentLines = \explode($phpcsFile->eolChar, $docComment); // @phpstan-ignore-line
5555

5656
foreach ($commentLines as $line) {
57-
if (\preg_match('/@var/', $line) && \count($commentLines) < 3) {
58-
$phpcsFile->addError(
59-
'The annotation @var can\'t be on one line for class property. Please use multiline.',
60-
$stackPtr,
61-
ErrorCodes::WRONG_FORMAT
62-
);
57+
if (\str_contains($line, '@var')) {
58+
if (3 > \count($commentLines)) {
59+
$phpcsFile->addError(
60+
'The annotation @var can\'t be on one line for class property. Please use multiline.',
61+
$stackPtr,
62+
ErrorCodes::WRONG_FORMAT
63+
);
64+
}
65+
66+
if (\str_contains($line, '[]')) {
67+
$phpcsFile->addError(
68+
'Please use vector type annotation for arrays.',
69+
$stackPtr,
70+
ErrorCodes::ARRAYS_DOC_VECTOR
71+
);
72+
}
6373
}
6474
}
6575
}

src/PhpCs/FiveLab/Sniffs/Commenting/CommentSniff.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@
2222
*/
2323
class CommentSniff extends AbstractFunctionDocCommentSniff
2424
{
25-
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName): void
25+
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName, int $countCommentLines): void
2626
{
27+
if (1 === $countCommentLines) {
28+
$phpcsFile->addErrorOnLine(
29+
'A method or function cannot have a single line comment.',
30+
$startLineNumber,
31+
ErrorCodes::WRONG_FORMAT
32+
);
33+
}
34+
2735
$startCommentLine = null;
2836
$startAnnotationLine = null;
2937

@@ -35,12 +43,12 @@ protected function processLines(File $phpcsFile, int $startLineNumber, array $li
3543
$existInheritdoc = true;
3644
}
3745

38-
if ($line && 0 !== \strpos($line, '@')) {
46+
if ($line && !\str_starts_with($line, '@')) {
3947
$endCommentLine = (int) $lineIndex;
4048
$startCommentLine = (int) ($startCommentLine ?? $lineIndex);
4149
}
4250

43-
if (0 === \strpos($line, '@')) {
51+
if (\str_starts_with($line, '@')) {
4452
$startAnnotationLine = (int) ($startAnnotationLine ?? $lineIndex);
4553
}
4654
}

src/PhpCs/FiveLab/Sniffs/Commenting/InheritdocSniff.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
class InheritdocSniff extends AbstractFunctionDocCommentSniff
2525
{
26-
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName): void
26+
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName, int $countCommentLines): void
2727
{
2828
$commentBeforeInheritdoc = null;
2929
$commentAfterInheritdoc = null;
@@ -66,13 +66,6 @@ protected function processLines(File $phpcsFile, int $startLineNumber, array $li
6666
}
6767
}
6868

69-
/**
70-
* Check what method exist parent declaration.
71-
*
72-
* @param File $phpcsFile
73-
* @param int $line
74-
* @param string $methodName
75-
*/
7669
private function assertExistDocInParents(File $phpcsFile, int $line, string $methodName): void
7770
{
7871
$declaredName = PhpCsUtils::getDeclaredClassName($phpcsFile);

src/PhpCs/FiveLab/Sniffs/Commenting/MagicMethodSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323
class MagicMethodSniff extends AbstractFunctionDocCommentSniff
2424
{
25-
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName): void
25+
protected function processLines(File $phpcsFile, int $startLineNumber, array $lines, string $functionName, int $countCommentLines): void
2626
{
27-
if (0 !== \strpos($functionName, '__')) {
27+
if (!\str_starts_with($functionName, '__')) {
2828
// Not a magic method.
2929
return;
3030
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/*
6+
* This file is part of the FiveLab CiRules package
7+
*
8+
* (c) FiveLab
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code
12+
*/
13+
14+
namespace FiveLab\Component\CiRules\PhpCs\FiveLab\Sniffs\Commenting;
15+
16+
use FiveLab\Component\CiRules\PhpCs\FiveLab\ErrorCodes;
17+
use PHP_CodeSniffer\Files\File;
18+
use PHP_CodeSniffer\Sniffs\Sniff;
19+
20+
class ProhibitedDocCommentsSniff implements Sniff
21+
{
22+
public function register(): array
23+
{
24+
return [
25+
T_DOC_COMMENT_OPEN_TAG,
26+
];
27+
}
28+
29+
public function process(File $phpcsFile, mixed $stackPtr): void
30+
{
31+
$tokens = $phpcsFile->getTokens();
32+
$commentToken = $tokens[$stackPtr];
33+
34+
$possiblyDeclaration = $phpcsFile->findNext([
35+
T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION,
36+
T_ABSTRACT, T_FINAL, T_ENUM
37+
], $commentToken['comment_closer'] + 1, local: true);
38+
39+
if ($possiblyDeclaration) {
40+
return;
41+
}
42+
43+
$varPtr = $phpcsFile->findNext(T_VARIABLE, $commentToken['comment_closer'] + 1, local: true);
44+
45+
if ($varPtr) {
46+
$commentTokenPtr = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($stackPtr + 1), $commentToken['comment_closer'], local: true);
47+
48+
if (false === $commentTokenPtr) {
49+
$phpcsFile->addError(
50+
'PHPDoc comment can contains only @var here.',
51+
$stackPtr,
52+
ErrorCodes::PHPDOC_NOT_ALLOWED
53+
);
54+
55+
return;
56+
}
57+
}
58+
59+
$isOnlyVarTag = $this->isHasOnlyVarTag($phpcsFile, $stackPtr, $commentToken['comment_closer'], $tokens);
60+
61+
if (true === $isOnlyVarTag) {
62+
return;
63+
}
64+
65+
$phpcsFile->addError(
66+
'PHPDoc comment is not allowed here.',
67+
$stackPtr,
68+
ErrorCodes::PHPDOC_NOT_ALLOWED
69+
);
70+
}
71+
72+
/**
73+
* Check has T_DOC_COMMENT_TAG another tags
74+
*
75+
* @param File $phpcsFile
76+
* @param int $startPtr
77+
* @param int $endPtr
78+
* @param array<int, mixed> $tokens
79+
*
80+
* @return bool|null
81+
*/
82+
private function isHasOnlyVarTag(File $phpcsFile, int $startPtr, int $endPtr, array $tokens): ?bool
83+
{
84+
$commentTokenPtr = $phpcsFile->findNext(T_DOC_COMMENT_TAG, ($startPtr + 1), $endPtr);
85+
86+
if (false === $commentTokenPtr) {
87+
return null;
88+
}
89+
90+
if ($commentTokenPtr && $tokens[$commentTokenPtr]['content'] !== '@var') {
91+
$phpcsFile->addError(
92+
\sprintf('PHPDoc comment tag %s is not allowed here.', $tokens[$commentTokenPtr]['content']),
93+
$commentTokenPtr,
94+
ErrorCodes::PHPDOC_NOT_ALLOWED
95+
);
96+
97+
return false;
98+
}
99+
100+
$this->isHasOnlyVarTag($phpcsFile, $commentTokenPtr, $endPtr, $tokens);
101+
102+
return true;
103+
}
104+
}

src/PhpCs/FiveLab/Sniffs/Commenting/ProhibitedInheritdocOnlySniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function process(File $phpcsFile, mixed $stackPtr): void
3535
$closeTokenPtr = $phpcsFile->findNext([T_DOC_COMMENT_CLOSE_TAG], $stackPtr);
3636

3737
$contentsBetweenPtrs = PhpCsUtils::getContentsBetweenPtrs($phpcsFile, $stackPtr, $closeTokenPtr + 1);
38-
$contentsBetweenPtrs = \str_replace([PHP_EOL, ' '], '', $contentsBetweenPtrs);
38+
$contentsBetweenPtrs = \str_replace([PHP_EOL, ' ', '*'], '', $contentsBetweenPtrs);
3939

40-
if ('/***{@inheritdoc}*/' === $contentsBetweenPtrs) {
40+
if ('/{@inheritdoc}/' === \strtolower($contentsBetweenPtrs)) {
4141
$phpcsFile->addError(
4242
'Prohibited {@inheritdoc} only comment.',
4343
$stackPtr,

0 commit comments

Comments
 (0)