Skip to content

Commit d36e8bf

Browse files
author
roettigl
committed
#21: WIP add rule for phpdoc
1 parent fde7fc5 commit d36e8bf

File tree

4 files changed

+264
-0
lines changed

4 files changed

+264
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento2\Sniffs\Commenting;
8+
9+
use Magento2\Sniffs\Legacy\MageEntitySniff;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Util\Tokens;
13+
14+
/**
15+
* Detects PHPDoc formatting for constants.
16+
*/
17+
class PHPDocFormattingSniff implements Sniff
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function register()
23+
{
24+
return [T_FUNCTION];
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function process(File $phpcsFile, $stackPtr)
31+
{
32+
$tokens = $phpcsFile->getTokens();
33+
34+
$funcParamStart = $tokens[$stackPtr]['parenthesis_opener'];
35+
$funcParamCloser = $tokens[$stackPtr]['parenthesis_closer'];
36+
$funcBodyStart = $tokens[$stackPtr]['scope_opener'];
37+
38+
// search for php7 return type declarations like func() : string {}
39+
$funcReturnTypePos = $phpcsFile->findNext([T_STRING], $funcParamCloser, $funcBodyStart);
40+
$funcParamType = null;
41+
if ($funcReturnTypePos !== false) {
42+
$funcReturnType = $tokens[$funcReturnTypePos]['content'];
43+
}
44+
45+
$funcParamTypeList = $this->getFunctionParameterWithType(
46+
array_slice(
47+
$tokens,
48+
$funcParamStart + 1,
49+
$funcParamCloser - $funcParamStart
50+
)
51+
);
52+
$paramType = null;
53+
54+
if (isset($funcReturnType) && count($funcParamTypeList) !== 0) {
55+
// function use php7 return type declarations - no check required
56+
return;
57+
}
58+
59+
// search for php doc block
60+
$find = Tokens::$methodPrefixes;
61+
$find[] = T_WHITESPACE;
62+
$commentEnd = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true);
63+
$commentStart = false;
64+
if($commentEnd !== false)
65+
{
66+
$commentStart = '';
67+
}
68+
}
69+
70+
71+
/**
72+
* Returns all parameter as list with there php 7 type declarations like
73+
* func(string $arg1, int $arg2)
74+
*
75+
* @param array $tokens
76+
* @return array
77+
*/
78+
private function getFunctionParameterWithType(array $tokens)
79+
{
80+
$paramType = null;
81+
$functionParameterList = [];
82+
83+
foreach ($tokens as $token) {
84+
$type = $token['code'];
85+
$content = $token['content'];
86+
87+
if ($type === T_COMMA) {
88+
$paramType = null;
89+
continue;
90+
}
91+
92+
if ($type === T_STRING) {
93+
$paramType = $content;
94+
continue;
95+
}
96+
97+
if ($type === T_VARIABLE && $paramType !== null) {
98+
$functionParameterList[] =
99+
[
100+
'content' => $content,
101+
'type' => $paramType
102+
];
103+
}
104+
}
105+
106+
return $functionParameterList;
107+
}
108+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class EverythingIsGoodHere
4+
{
5+
/**
6+
* @param string $arg1
7+
* @param bool $arg2
8+
*/
9+
private function missingArgumentTypeSoParamsNeedToBeAdded($arg1, $arg2): bool
10+
{
11+
}
12+
13+
/**
14+
* @return bool
15+
*/
16+
private function presentArgumentTypeSoAnnotationNotNeededButMissingReturnType(string $arg1, bool $arg2)
17+
{
18+
}
19+
20+
private function presentArgumentAndReturnTypes(string $arg1, bool $arg2): bool
21+
{
22+
}
23+
24+
/**
25+
* @throws Exception
26+
*/
27+
private function throwsAnException(): void
28+
{
29+
throw new \Exception();
30+
}
31+
32+
/**
33+
* We have an additional wonderful message here.
34+
*
35+
* @return void
36+
*/
37+
private function presentGoodDescription(string $arg1, bool $arg2)
38+
{
39+
}
40+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
class EverythingIsBadHere
4+
{
5+
/**
6+
* @param bool $arg2
7+
* @param string $arg1
8+
*/
9+
private function wrongParamOrder($arg1, $arg2): bool
10+
{
11+
12+
}
13+
14+
/**
15+
* @param bool $arg2
16+
*/
17+
private function missingParamAnnotation($arg1, $arg2): bool
18+
{
19+
20+
}
21+
22+
private function missingArgumentAnnotations($arg1, $arg2): bool
23+
{
24+
25+
}
26+
27+
/**
28+
* @param string $arg1
29+
* @param bool $arg2
30+
* @param bool $arg2
31+
*/
32+
private function paramDuplication($arg1, $arg2): bool
33+
{
34+
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
private function inheritDocShouldNotBeUsed(string $arg1, bool $arg2)
41+
{
42+
43+
}
44+
45+
/**
46+
* @param $arg1
47+
* @param $arg2
48+
*/
49+
private function missingParamType($arg1, $arg2): bool
50+
{
51+
52+
}
53+
54+
/**
55+
* Useless description
56+
*/
57+
private function uselessDescription(string $arg1, bool $arg2): bool
58+
{
59+
60+
}
61+
62+
/**
63+
*
64+
*/
65+
private function dockBlockIsEmpty(string $arg1, bool $arg2): bool
66+
{
67+
68+
}
69+
70+
/**
71+
*
72+
*
73+
* @param string $arg1
74+
* @param bool $arg2
75+
*
76+
*/
77+
private function redundantLines($arg1, $arg2): bool
78+
{
79+
80+
}
81+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Commenting;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class ConstantsPHPDocFormattingUnitTest
12+
*/
13+
class PHPDocFormattingUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function getWarningList($testFile = '')
27+
{
28+
if ($testFile === 'PHPDocFormattingUnitTest.1.inc') {
29+
return [];
30+
}
31+
32+
33+
return [];
34+
}
35+
}

0 commit comments

Comments
 (0)