Skip to content

Commit 4f2fd32

Browse files
author
Lars Roettig
committed
#22: Impl. Abstract classes MUST NOT be marked as public
1 parent df5fc07 commit 4f2fd32

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Sniffs\Classes;
8+
9+
use PHP_CodeSniffer\Sniffs\Sniff;
10+
use PHP_CodeSniffer\Files\File;
11+
12+
/**
13+
* Detects api annotation for an abstract class.
14+
*/
15+
class AbstractApiSniff implements Sniff
16+
{
17+
18+
/**
19+
* String representation of warning.
20+
*
21+
* @var string
22+
*/
23+
protected $warningMessage = 'Abstract classes MUST NOT be marked as public @api.';
24+
25+
/**
26+
* Warning violation code.
27+
*
28+
* @var string
29+
*/
30+
protected $warningCode = 'AbstractApi';
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function register()
36+
{
37+
return [T_CLASS];
38+
}
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function process(File $phpcsFile, $stackPtr)
44+
{
45+
$tokens = $phpcsFile->getTokens();
46+
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
47+
if ($prev !== false && $tokens[$prev]['code'] !== T_ABSTRACT) {
48+
return;
49+
}
50+
51+
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
52+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
53+
54+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
55+
$token = $tokens[$i];
56+
57+
if ($token['code'] !== T_DOC_COMMENT_TAG) {
58+
continue;
59+
}
60+
61+
if (strpos($token['content'], '@api') === false) {
62+
continue;
63+
}
64+
65+
$phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode);
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Interface FooInterface
5+
* @api
6+
*/
7+
interface FooInterface
8+
{
9+
public function execute();
10+
}
11+
12+
/**
13+
* // Rule find: api annotation for this class
14+
* @api
15+
*/
16+
abstract class Foo implements FooInterface
17+
{
18+
19+
}
20+
21+
/**
22+
* // Rule find: api annotation for this class
23+
* @api
24+
*/
25+
abstract
26+
class FooBar implements FooInterface
27+
{
28+
29+
}
30+
31+
32+
/**
33+
* Class Bar
34+
*/
35+
class Bar extends Foo
36+
{
37+
public function execute()
38+
{
39+
// TODO: Implement execute() method.
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Tests\Classes;
8+
9+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
10+
11+
/**
12+
* Class ObjectInstantiationUnitTest
13+
*/
14+
class AbstractApiUnitTest extends AbstractSniffUnitTest
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function getErrorList()
20+
{
21+
return [];
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function getWarningList()
28+
{
29+
return [
30+
14 => 1,
31+
23 => 1
32+
];
33+
}
34+
}

Magento/ruleset.xml

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@
142142
<severity>8</severity>
143143
<type>warning</type>
144144
</rule>
145+
<rule ref="Magento.Classes.AbstractApi">
146+
<severity>8</severity>
147+
<type>warning</type>
148+
</rule>
145149

146150
<!-- Severity 7 warnings: General code issues. -->
147151
<rule ref="Generic.Arrays.DisallowLongArraySyntax">

0 commit comments

Comments
 (0)