Skip to content

Commit fd79554

Browse files
Merge pull request #100 from magento-commerce/imported-eliseacornejo-magento-coding-standard-312
[Imported] AC-672: Create phpcs static check for LicenseTest
2 parents 3b0e492 + ef49c1b commit fd79554

8 files changed

+167
-0
lines changed
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
/**
17+
* A list of tokenizers this sniff supports.
18+
*
19+
* @var array
20+
*/
21+
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS, 'PHP'];
22+
23+
private const WARNING_CODE = 'FoundLegacyTextInCopyright';
24+
25+
private const LEGACY_TEXTS = ['Irubin Consulting Inc', 'DBA Varien', 'Magento Inc'];
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function register()
31+
{
32+
return [
33+
T_DOC_COMMENT_STRING,
34+
T_INLINE_HTML
35+
];
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function process(File $phpcsFile, $stackPtr)
42+
{
43+
$tokens = $phpcsFile->getTokens();
44+
$content = null;
45+
46+
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING) {
47+
$content = $tokens[$stackPtr]['content'];
48+
}
49+
if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) {
50+
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
51+
}
52+
if ($content != null) {
53+
$this->checkLicense($content, $stackPtr, $phpcsFile);
54+
}
55+
}
56+
57+
/**
58+
* Check that the copyright license does not contain legacy text
59+
*
60+
* @param string $content
61+
* @param int $stackPtr
62+
* @param File $phpcsFile
63+
*/
64+
private function checkLicense(string $content, int $stackPtr, File $phpcsFile): void
65+
{
66+
$commentContent = $content;
67+
if (stripos($commentContent, 'copyright') === false) {
68+
return;
69+
}
70+
foreach (self::LEGACY_TEXTS as $legacyText) {
71+
if (stripos($commentContent, $legacyText) !== false) {
72+
$phpcsFile->addWarning(
73+
sprintf("The copyright license contains legacy text: %s.", $legacyText),
74+
$stackPtr,
75+
self::WARNING_CODE
76+
);
77+
}
78+
}
79+
}
80+
}
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+
*/
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+
*/
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>
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>
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+
*/
+49
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

+4
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@
334334
<severity>8</severity>
335335
<type>warning</type>
336336
</rule>
337+
<rule ref="Magento2.Legacy.License">
338+
<severity>8</severity>
339+
<type>warning</type>
340+
</rule>
337341

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

0 commit comments

Comments
 (0)