Skip to content

Commit 08c7084

Browse files
committed
AC-669: Create phpcs static check for InstallUpgradeTest
1 parent c5dbf03 commit 08c7084

16 files changed

+213
-1227
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 InstallUpgradeSniff implements Sniff
14+
{
15+
private const ERROR_CODE = 'obsoleteScript';
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function register(): array
21+
{
22+
return [
23+
T_OPEN_TAG
24+
];
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function process(File $phpcsFile, $stackPtr)
31+
{
32+
if ($stackPtr > 0) {
33+
return;
34+
}
35+
36+
if (strpos(basename($phpcsFile->getFilename()), 'install-') === 0) {
37+
$phpcsFile->addError(
38+
'Install scripts are obsolete. '
39+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
40+
0,
41+
self::ERROR_CODE
42+
);
43+
}
44+
45+
if (strpos(basename($phpcsFile->getFilename()), 'InstallSchema') === 0) {
46+
$phpcsFile->addError(
47+
'InstallSchema scripts are obsolete. '
48+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
49+
0,
50+
self::ERROR_CODE
51+
);
52+
}
53+
54+
if (strpos(basename($phpcsFile->getFilename()), 'InstallData') === 0) {
55+
$phpcsFile->addError(
56+
'InstallData scripts are obsolete. '
57+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
58+
0,
59+
self::ERROR_CODE
60+
);
61+
}
62+
63+
if (strpos(basename($phpcsFile->getFilename()), 'data-install-') === 0) {
64+
$phpcsFile->addError(
65+
'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder',
66+
0,
67+
self::ERROR_CODE
68+
);
69+
}
70+
71+
if (strpos(basename($phpcsFile->getFilename()), 'upgrade-') === 0) {
72+
$phpcsFile->addError(
73+
'Upgrade scripts are obsolete. '
74+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
75+
0,
76+
self::ERROR_CODE
77+
);
78+
}
79+
80+
if (strpos(basename($phpcsFile->getFilename()), 'UpgradeSchema') === 0) {
81+
$phpcsFile->addError(
82+
'UpgradeSchema scripts are obsolete. '
83+
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
84+
0,
85+
self::ERROR_CODE
86+
);
87+
}
88+
89+
if (strpos(basename($phpcsFile->getFilename()), 'UpgradeData') === 0) {
90+
$phpcsFile->addError(
91+
'UpgradeSchema scripts are obsolete. '
92+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
93+
0,
94+
self::ERROR_CODE
95+
);
96+
}
97+
98+
if (strpos(basename($phpcsFile->getFilename()), 'data-upgrade-') === 0) {
99+
$phpcsFile->addError(
100+
'Upgrade scripts are obsolete. '
101+
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
102+
0,
103+
self::ERROR_CODE
104+
);
105+
}
106+
107+
if (strpos(basename($phpcsFile->getFilename()), 'recurring') === 0) {
108+
$phpcsFile->addError(
109+
'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder',
110+
0,
111+
self::ERROR_CODE
112+
);
113+
}
114+
115+
if (preg_match('/(sql|data)/', dirname($phpcsFile->getFilename())) === 1) {
116+
$phpcsFile->addError(
117+
$phpcsFile->getFilename()." is in an invalid directory ".dirname($phpcsFile->getFilename()).":\n"
118+
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
119+
. "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes.",
120+
0,
121+
self::ERROR_CODE
122+
);
123+
}
124+
}
125+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 DirectoryIterator;
9+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
10+
use RecursiveDirectoryIterator;
11+
use RecursiveIteratorIterator;
12+
13+
class InstallUpgradeUnitTest extends AbstractSniffUnitTest
14+
{
15+
private $wrongFileNames = [
16+
'data-install-.inc',
17+
'data-upgrade-.inc',
18+
'install-sample.inc',
19+
'InstallData.inc',
20+
'InstallSchema.inc',
21+
'recurring.inc',
22+
'upgrade-.inc',
23+
'UpgradeData.inc',
24+
'UpgradeSchema.inc',
25+
'file.inc',
26+
'file2.inc',
27+
];
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
protected function getTestFiles($testFileBase): array
33+
{
34+
$testFiles = [];
35+
36+
$dir = __DIR__.'/_files/InstallUpgradeUnitTest';
37+
$di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
38+
39+
foreach ($di as $file) {
40+
$path = $file->getPathname();
41+
if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') {
42+
$testFiles[] = $path;
43+
}
44+
}
45+
46+
// Put them in order.
47+
sort($testFiles);
48+
49+
return $testFiles;
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function getErrorList($testFile = '')
56+
{
57+
if (in_array($testFile, $this->wrongFileNames)) {
58+
return [
59+
1 => 1
60+
];
61+
}
62+
return [];
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
public function getWarningList()
69+
{
70+
return [];
71+
}
72+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

0 commit comments

Comments
 (0)