Skip to content

Commit 8b2cd90

Browse files
committed
AC-666: Create phpcs static check for CopyrightTest
1 parent e3f4d87 commit 8b2cd90

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
class CopyrightSniff implements Sniff
14+
{
15+
private const WARNING_CODE = 'FoundCopyrightMissingOrWrongFormat';
16+
17+
private const COPYRIGHT_MAGENTO_TEXT = 'Copyright © Magento, Inc. All rights reserved.';
18+
private const COPYRIGHT_ADOBE = '/Copyright \d+ Adobe/';
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function register()
24+
{
25+
return [T_OPEN_TAG];
26+
}
27+
28+
public function process(File $phpcsFile, $stackPtr)
29+
{
30+
$positionOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1);
31+
32+
if ($positionOpenTag === false){
33+
$positionComment = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $stackPtr);
34+
$contentFile = $phpcsFile->getTokens()[$positionComment]['content'];
35+
$adobeCopyrightFound = preg_match(self::COPYRIGHT_ADOBE, $contentFile);
36+
37+
if (strpos($contentFile, self::COPYRIGHT_MAGENTO_TEXT) === false || $adobeCopyrightFound === false) {
38+
$phpcsFile->addWarningOnLine(
39+
'Copyright is missing or has wrong format',
40+
null,
41+
self::WARNING_CODE
42+
);
43+
}
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights different text.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<?php
8+
/**
9+
* Another comment
10+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
/**
3+
* Copyright Adobe 2020
4+
*/
5+
?>
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. 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 CopyrightUnitTest 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 === 'CopyrightUnitTest.4.inc') {
26+
return [];
27+
}
28+
29+
return [
30+
null => 1,
31+
];
32+
}
33+
}

0 commit comments

Comments
 (0)