Skip to content
128 changes: 128 additions & 0 deletions Magento2/Sniffs/Legacy/InstallUpgradeSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento2\Sniffs\Legacy;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SplFileInfo;

class InstallUpgradeSniff implements Sniff
{
private const ERROR_CODE = 'obsoleteScript';

/**
* @inheritdoc
*/
public function register(): array
{
return [
T_OPEN_TAG
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($stackPtr > 0) {
return;
}

$fileInfo = new SplFileInfo($phpcsFile->getFilename());

if (strpos($fileInfo->getFilename(), 'install-') === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are doing the same logic with different data several times. Could you extract a method to simplify it?

$phpcsFile->addError(
'Install scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'InstallSchema') === 0) {
$phpcsFile->addError(
'InstallSchema scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'InstallData') === 0) {
$phpcsFile->addError(
'InstallData scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'data-install-') === 0) {
$phpcsFile->addError(
'Install scripts are obsolete. Please create class InstallData in module\'s Setup folder',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'upgrade-') === 0) {
$phpcsFile->addError(
'Upgrade scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'UpgradeSchema') === 0) {
$phpcsFile->addError(
'UpgradeSchema scripts are obsolete. '
. 'Please use declarative schema approach in module\'s etc/db_schema.xml file',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'UpgradeData') === 0) {
$phpcsFile->addError(
'UpgradeSchema scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'data-upgrade-') === 0) {
$phpcsFile->addError(
'Upgrade scripts are obsolete. '
. 'Please use data patches approach in module\'s Setup/Patch/Data dir',
0,
self::ERROR_CODE
);
}

if (strpos($fileInfo->getFilename(), 'recurring') === 0) {
$phpcsFile->addError(
'Recurring scripts are obsolete. Please create class Recurring in module\'s Setup folder',
0,
self::ERROR_CODE
);
}

if (preg_match('/(sql|data)/', $fileInfo->getPath()) === 1) {
$phpcsFile->addError(
$fileInfo->getFilename()." is in an invalid directory ".$fileInfo->getPath().":\n"
. "- Create a data patch within module's Setup/Patch/Data folder for data upgrades.\n"
. "- Use declarative schema approach in module's etc/db_schema.xml file for schema changes.",
0,
self::ERROR_CODE
);
}
}
}
78 changes: 78 additions & 0 deletions Magento2/Tests/Legacy/InstallUpgradeUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use DirectoryIterator;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

class InstallUpgradeUnitTest extends AbstractSniffUnitTest
{
private $wrongFileNames = [
'data-install-.inc',
'data-upgrade-.inc',
'install-sample.inc',
'InstallData.inc',
'InstallSchema.inc',
'recurring.inc',
'upgrade-.inc',
'UpgradeData.inc',
'UpgradeSchema.inc',
'file.inc',
'file2.inc',
];

/**
* @inheritdoc
*/
protected function getTestFiles($testFileBase): array
{
$testFiles = [];

$dir = __DIR__.'/_files/InstallUpgradeUnitTest';
$di = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

/**
* @var DirectoryIterator $file
*/
foreach ($di as $file) {
if ($file->isDir()) {
continue;
}
$path = $file->getPathname();
if ($path !== $testFileBase.'php' && substr($path, -5) !== 'fixed' && substr($path, -4) !== '.bak') {
$testFiles[] = $path;
}
}

// Put them in order.
sort($testFiles);

return $testFiles;
}

/**
* @inheritdoc
*/
public function getErrorList($testFile = '')
{
if (in_array($testFile, $this->wrongFileNames)) {
return [
1 => 1
];
}
return [];
}

/**
* @inheritdoc
*/
public function getWarningList()
{
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
5 changes: 4 additions & 1 deletion Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@
<severity>10</severity>
<type>error</type>
</rule>
<rule ref="Magento2.Legacy.InstallUpgrade">
<severity>10</severity>
<type>error</type>
</rule>
<rule ref="Magento2.PHP.AutogeneratedClassNotInConstructor">
<include-pattern>*\.php$</include-pattern>
<severity>10</severity>
<type>error</type>
</rule>

<rule ref="Magento2.Html.HtmlSelfClosingTags">
<severity>10</severity>
<type>error</type>
Expand Down