-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathLicenseSniff.php
79 lines (70 loc) · 2.03 KB
/
LicenseSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento2Framework\Sniffs\Header;
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 = ['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
);
}
}
}
}