Skip to content

Commit 7fdd6b4

Browse files
committed
AC-672: Create phpcs static check for LicenseTest
1 parent 3c477b1 commit 7fdd6b4

File tree

8 files changed

+158
-0
lines changed

8 files changed

+158
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
namespace Magento2\Sniffs\Legacy;
9+
10+
use Magento2\Sniffs\Less\TokenizerSymbolsInterface;
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
14+
class LicenseSniff implements Sniff
15+
{
16+
private const WARNING_CODE = 'FoundLegacyTextInCopyright';
17+
18+
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS, 'PHP'];
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function register()
24+
{
25+
return [
26+
T_DOC_COMMENT_STRING,
27+
T_INLINE_HTML
28+
];
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function process(File $phpcsFile, $stackPtr)
35+
{
36+
$tokens = $phpcsFile->getTokens();
37+
$content = null;
38+
39+
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING) {
40+
$content = $tokens[$stackPtr]['content'];
41+
}
42+
if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) {
43+
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
44+
}
45+
if ($content != null){
46+
$this->checkLicense($content, $stackPtr, $phpcsFile);
47+
}
48+
}
49+
50+
/**
51+
* @param string $content
52+
* @param int $stackPtr
53+
* @param File $phpcsFile
54+
*/
55+
private function checkLicense(string $content, int $stackPtr, File $phpcsFile): void
56+
{
57+
$commentContent = $content;
58+
if (stripos($commentContent, 'copyright') === false) {
59+
return;
60+
}
61+
foreach (['Irubin Consulting Inc', 'DBA Varien', 'Magento Inc'] as $legacyText) {
62+
if (stripos($commentContent, $legacyText) !== false) {
63+
$phpcsFile->addWarning(
64+
sprintf("The copyright license contains legacy text: %s.", $legacyText),
65+
$stackPtr,
66+
self::WARNING_CODE
67+
);
68+
}
69+
}
70+
}
71+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/**
3+
* Copyright © Magento Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright My testing text
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<tag>
9+
10+
</tag>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* @copyright DBA Varien
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<tag>
9+
10+
</tag>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @copyright Copyright Irubin Consulting Inc
3+
* See COPYING.txt for license details.
4+
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Legacy;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
class LicenseUnitTest extends AbstractSniffUnitTest
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
public function getErrorList(): array
16+
{
17+
return [];
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getWarningList($testFile = ''): array
24+
{
25+
if ($testFile === 'LicenseUnitTest.1.inc' || $testFile === 'LicenseUnitTest.3.xml') {
26+
return [];
27+
}
28+
29+
if ($testFile === 'LicenseUnitTest.2.inc') {
30+
return [
31+
3 => 1,
32+
];
33+
}
34+
35+
if ($testFile === 'LicenseUnitTest.4.xml') {
36+
return [
37+
4 => 1,
38+
];
39+
}
40+
41+
if ($testFile === 'LicenseUnitTest.5.less') {
42+
return [
43+
2 => 1,
44+
];
45+
}
46+
47+
return [];
48+
}
49+
}

Magento2/ruleset.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@
194194
<type>warning</type>
195195
<exclude-pattern>*\.xml$</exclude-pattern>
196196
</rule>
197+
<rule ref="Magento2.Legacy.License">
198+
<severity>9</severity>
199+
<type>warning</type>
200+
</rule>
197201

198202
<!-- Severity 8 warnings: Magento specific code issues and design violations. -->
199203
<rule ref="Magento2.Classes.AbstractApi">

0 commit comments

Comments
 (0)