Skip to content

AC-664: Create phpcs static check for CodingStandardsIgnoreAnnotationUsageTest #257

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

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Legacy;

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

class CodingStandardsIgnoreAnnotationUsageSniff implements Sniff
{
private const CODING_STANDARDS_IGNORE_FILE = '@codingStandardsIgnoreFile';

private const WARNING_CODE = 'avoidAnnotation';

private const WARNING_MESSAGE = '@codingStandardsIgnoreFile annotation must be avoided. '
. 'Use codingStandardsIgnoreStart/codingStandardsIgnoreEnd to suppress code fragment '
. 'or use codingStandardsIgnoreLine to suppress line. ';

/**
* @inheritDoc
*/
public function register(): array
{
return [
T_COMMENT
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if (strpos($tokens[$stackPtr - 1]['content'], self::CODING_STANDARDS_IGNORE_FILE) !== false) {
$phpcsFile->addWarning(
self::WARNING_MESSAGE . $phpcsFile->getFilename(),
$stackPtr,
self::WARNING_CODE
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile
class Sample
{
/**
* @var int
*/
private $randomNumber;

/**
* @throws Exception
*/
public function __construct() {
$this->randomNumber = random_int(0, 999);
}

/**
* @return int
*/
public function getRandomNumber(): int
{
return $this->randomNumber;
}

/**
* @param int $randomNumber
*/
public function setRandomNumber(int $randomNumber): void
{
$this->randomNumber = $randomNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class CodingStandardsIgnoreAnnotationUsageUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be an error at line 7, that's where @codingStandardsIgnoreFile is set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, i set it up empty because was expecting complains on command line when running vendor/bin/phpunit.
Actually, it must be a warning.

}

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

<!-- Severity 9 warnings: Possible security and issues that may cause bugs. -->
<rule ref="Magento2.Legacy.CodingStandardsIgnoreAnnotationUsage">
<severity>9</severity>
<type>warning</type>
</rule>
<rule ref="Generic.Files.ByteOrderMark">
<severity>9</severity>
<type>warning</type>
Expand Down