Skip to content
80 changes: 80 additions & 0 deletions Magento2/Sniffs/Legacy/LicenseSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento2\Sniffs\Legacy;

use Magento2\Sniffs\Less\TokenizerSymbolsInterface;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class LicenseSniff implements Sniff
{
/**
* A list of tokenizers this sniff supports.
*
* @var array
*/
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS, 'PHP'];

private const WARNING_CODE = 'FoundLegacyTextInCopyright';

private const LEGACY_TEXTS = ['Irubin Consulting Inc', 'DBA Varien', 'Magento Inc'];

/**
* @inheritdoc
*/
public function register()
{
return [
T_DOC_COMMENT_STRING,
T_INLINE_HTML
];
}

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

if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING) {
$content = $tokens[$stackPtr]['content'];
}
if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) {
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
}
if ($content != null) {
$this->checkLicense($content, $stackPtr, $phpcsFile);
}
}

/**
* Check that the copyright license does not contain legacy text
*
* @param string $content
* @param int $stackPtr
* @param File $phpcsFile
*/
private function checkLicense(string $content, int $stackPtr, File $phpcsFile): void
{
$commentContent = $content;
if (stripos($commentContent, 'copyright') === false) {
return;
}
foreach (self::LEGACY_TEXTS as $legacyText) {
if (stripos($commentContent, $legacyText) !== false) {
$phpcsFile->addWarning(
sprintf("The copyright license contains legacy text: %s.", $legacyText),
$stackPtr,
self::WARNING_CODE
);
}
}
}
}
5 changes: 5 additions & 0 deletions Magento2/Tests/Legacy/LicenseUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
5 changes: 5 additions & 0 deletions Magento2/Tests/Legacy/LicenseUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/**
* Copyright © Magento Inc. All rights reserved.
* See COPYING.txt for license details.
*/
10 changes: 10 additions & 0 deletions Magento2/Tests/Legacy/LicenseUnitTest.3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright My testing text
* See COPYING.txt for license details.
*/
-->
<tag>

</tag>
10 changes: 10 additions & 0 deletions Magento2/Tests/Legacy/LicenseUnitTest.4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* @copyright DBA Varien
* See COPYING.txt for license details.
*/
-->
<tag>

</tag>
4 changes: 4 additions & 0 deletions Magento2/Tests/Legacy/LicenseUnitTest.5.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @copyright Copyright Irubin Consulting Inc
* See COPYING.txt for license details.
*/
49 changes: 49 additions & 0 deletions Magento2/Tests/Legacy/LicenseUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class LicenseUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList(): array
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = ''): array
{
if ($testFile === 'LicenseUnitTest.1.inc' || $testFile === 'LicenseUnitTest.3.xml') {
return [];
}

if ($testFile === 'LicenseUnitTest.2.inc') {
return [
3 => 1,
];
}

if ($testFile === 'LicenseUnitTest.4.xml') {
return [
4 => 1,
];
}

if ($testFile === 'LicenseUnitTest.5.less') {
return [
2 => 1,
];
}

return [];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.License">
<severity>8</severity>
<type>warning</type>
</rule>

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