Skip to content

#22: Impl. Abstract classes MUST NOT be marked as public #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2019
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
68 changes: 68 additions & 0 deletions Magento/Sniffs/Classes/AbstractApiSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sniffs\Classes;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

/**
* Detects api annotation for an abstract class.
*/
class AbstractApiSniff implements Sniff
{

/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'Abstract classes MUST NOT be marked as public @api.';

/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'AbstractApi';

/**
* @inheritDoc
*/
public function register()
{
return [T_CLASS];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
if ($prev !== false && $tokens[$prev]['code'] !== T_ABSTRACT) {
return;
}

$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, 0);
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];

for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
$token = $tokens[$i];

if ($token['code'] !== T_DOC_COMMENT_TAG) {
continue;
}

if (strpos($token['content'], '@api') === false) {
continue;
}

$phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode);
}
}
}
41 changes: 41 additions & 0 deletions Magento/Tests/Classes/AbstractApiUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Interface FooInterface
* @api
*/
interface FooInterface
{
public function execute();
}

/**
* // Rule find: api annotation for this class
* @api
*/
abstract class Foo implements FooInterface
{

}

/**
* // Rule find: api annotation for this class
* @api
*/
abstract
class FooBar implements FooInterface
{

}


/**
* Class Bar
*/
class Bar extends Foo
{
public function execute()
{
// TODO: Implement execute() method.
}
}
34 changes: 34 additions & 0 deletions Magento/Tests/Classes/AbstractApiUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Tests\Classes;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Class ObjectInstantiationUnitTest
*/
class AbstractApiUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList()
{
return [
14 => 1,
23 => 1
];
}
}
4 changes: 4 additions & 0 deletions Magento/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
</rule>

<!-- Severity 8 warnings: Magento specific code issues and design violations. -->
<rule ref="Magento.Classes.AbstractApi">
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento.Classes.ObjectInstantiation">
<severity>8</severity>
<type>warning</type>
Expand Down