Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
327c5bb
AC-667 Create phpcs static check for EmailTemplateTest Fix
jcuerdo Sep 28, 2021
49f3d36
Merge branch 'develop' of https://github.com/magento/magento-coding-s…
jcuerdo Sep 28, 2021
8f9269d
Merge pull request #81 from magento-commerce/imported-jcuerdo-magento…
jcuerdo Sep 29, 2021
247578e
AC-679: Create phpcs static check for ObsoleteSystemConfigurationTest
eliseacornejo Sep 29, 2021
ffdf5a8
AC-674: Create phpcs static check for ObsoleteAclTest
loginesta Sep 29, 2021
5148a61
AC-679: Create phpcs static check for ObsoleteSystemConfigurationTest
eliseacornejo Sep 29, 2021
a91d371
AC-677: Create phpcs static test for ObsoleteMenuTest
loginesta Sep 29, 2021
b2111fa
Merge pull request #82 from magento-commerce/imported-loginesta-magen…
loginesta Sep 29, 2021
0207f9d
Merge branch 'develop' into AC-677
loginesta Sep 29, 2021
3771108
Merge pull request #83 from magento-commerce/imported-loginesta-magen…
loginesta Sep 30, 2021
4356c7f
Merge branch 'develop' of github.com:magento/magento-coding-standard …
eliseacornejo Sep 30, 2021
f0af108
Merge branch 'develop' of github.com:magento/magento-coding-standard …
eliseacornejo Sep 30, 2021
dc56f12
Merge pull request #84 from magento-commerce/imported-eliseacornejo-m…
eliseacornejo Sep 30, 2021
9f77a7f
AC-1159: Move eslint tests to magento-coding-standard
loginesta Sep 30, 2021
79b6b3d
Merge branch 'AC-1159' of github.com:loginesta/magento-coding-standar…
loginesta Sep 30, 2021
72bd288
Merge pull request #85 from magento-commerce/AC-205
loginesta Oct 1, 2021
cd9083c
AC-682: Create phpcs static check for RestrictedCodeTest
svera Oct 1, 2021
d775713
AC-682: Create phpcs static check for RestrictedCodeTest
svera Oct 1, 2021
6cdc855
Run JSCS against eslint/rules/ and Magento2/
loginesta Oct 1, 2021
6057057
AC-682: Create phpcs static check for RestrictedCodeTest
svera Oct 1, 2021
082e6b8
Merge pull request #86 from magento-commerce/imported-svera-magento-c…
svera Oct 1, 2021
ad59de1
Merge pull request #87 from magento-commerce/imported-loginesta-magen…
loginesta Oct 1, 2021
ea85ae0
AC-1355: Move JSCS into magento-coding-standards repo
loginesta Oct 1, 2021
a635424
Update README file
loginesta Oct 1, 2021
7e05e7d
Merge branch 'develop' of https://github.com/magento/magento-coding-s…
jcuerdo Oct 4, 2021
415b18f
Merge pull request #88 from magento-commerce/jscs
loginesta Oct 4, 2021
b9dc92e
AC-679: Fix ruleset for ObsoleteSystemConfigurationTest
jcuerdo Oct 4, 2021
19a26a7
Merge pull request #89 from magento-commerce/AC-679-FixRuleset
jcuerdo Oct 4, 2021
9ebe790
AC-1355: Move JSCS into magento-coding-standards repo - Fix indentation
loginesta Oct 5, 2021
914dd7a
Merge pull request #90 from magento-commerce/imported-loginesta-magen…
loginesta Oct 5, 2021
025a3e3
Increased version to 13
jcuerdo Oct 5, 2021
aa0c657
Merge pull request #91 from magento-commerce/release-13
jcuerdo Oct 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ jobs:
- name: Install dependencies
run: npm install
- name: Run ESLint
run: npm run eslint -- eslint/rules
run: npm run eslint -- eslint/rules Magento2 --ignore-pattern 'Magento2/Tests/Eslint/*Test.js'
- name: Run JSCS
run: npm run jscs eslint/rules Magento2

- name: Validate composer
run: composer validate
Expand Down
62 changes: 62 additions & 0 deletions Magento2/Sniffs/Legacy/ObsoleteAclSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\Legacy;

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

/**
* Test to find obsolete acl declaration
*/
class ObsoleteAclSniff implements Sniff
{
private const WARNING_OBSOLETE_ACL_STRUCTURE = 'ObsoleteAclStructure';

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

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

$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
$foundElements = $xml->xpath('/config/acl/*[boolean(./children) or boolean(./title)]');
foreach ($foundElements as $element) {
$phpcsFile->addWarning(
'Obsolete acl structure detected in line ' . dom_import_simplexml($element)->getLineNo(),
dom_import_simplexml($element)->getLineNo() - 1,
self::WARNING_OBSOLETE_ACL_STRUCTURE
);
}
}

/**
* Format the incoming XML to avoid tags split into several lines.
*
* @param File $phpcsFile
* @return false|string
*/
private function getFormattedXML(File $phpcsFile)
{
$doc = new DomDocument('1.0');
$doc->formatOutput = true;
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
return $doc->saveXML();
}
}
67 changes: 67 additions & 0 deletions Magento2/Sniffs/Legacy/ObsoleteMenuSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\Legacy;

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

/**
* Test to find obsolete acl declaration
*/
class ObsoleteMenuSniff implements Sniff
{
private const WARNING_OBSOLETE_MENU_STRUCTURE = 'ObsoleteMenuStructure';

/**
* @var string
*/
private $xpath = '/config/menu/*[boolean(./children) or boolean(./title) or boolean(./action)]';

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

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

$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
$foundElements = $xml->xpath($this->xpath);
foreach ($foundElements as $element) {
$phpcsFile->addWarning(
'Obsolete menu structure detected in line ' . dom_import_simplexml($element)->getLineNo(),
dom_import_simplexml($element)->getLineNo() - 1,
self::WARNING_OBSOLETE_MENU_STRUCTURE
);
}
}

/**
* Format the incoming XML to avoid tags split into several lines.
*
* @param File $phpcsFile
* @return false|string
*/
private function getFormattedXML(File $phpcsFile)
{
$doc = new DomDocument('1.0');
$doc->formatOutput = true;
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
return $doc->saveXML();
}
}
91 changes: 91 additions & 0 deletions Magento2/Sniffs/Legacy/ObsoleteSystemConfigurationSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Legacy;

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

class ObsoleteSystemConfigurationSniff implements Sniff
{
private const ERROR_CODE_XML = 'WrongXML';
private const WARNING_CODE_OBSOLETE = 'FoundObsoleteSystemConfiguration';

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

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

$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));

if ($xml === false) {
$this->invalidXML($phpcsFile, $stackPtr);
return;
}

$foundElements = $xml->xpath('/config/tabs|/config/sections');

if ($foundElements === false) {
return;
}

foreach ($foundElements as $element) {
$phpcsFile->addWarning(
"Obsolete system configuration structure detected in file.",
dom_import_simplexml($element)->getLineNo() - 1,
self::WARNING_CODE_OBSOLETE
);
}
}

/**
* Adds an invalid XML error
*
* @param File $phpcsFile
* @param int $stackPtr
*/
private function invalidXML(File $phpcsFile, int $stackPtr): void
{
$phpcsFile->addError(
sprintf(
"Couldn't parse contents of '%s', check that they are in valid XML format.",
$phpcsFile->getFilename(),
),
$stackPtr,
self::ERROR_CODE_XML
);
}

/**
* Format the incoming XML to avoid tags split into several lines.
*
* @param File $phpcsFile
* @return false|string
*/
private function getFormattedXML(File $phpcsFile)
{
$doc = new DomDocument('1.0');
$doc->formatOutput = true;
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999));
return $doc->saveXML();
}
}
134 changes: 134 additions & 0 deletions Magento2/Sniffs/Legacy/RestrictedCodeSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento2\Sniffs\Legacy;

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

/**
* Tests to find usage of restricted code
*/
class RestrictedCodeSniff implements Sniff
{
private const ERROR_MESSAGE = "Class '%s' is restricted in %s. Suggested replacement: %s";
private const ERROR_CODE = "restrictedClass";

/**
* List of fixtures that contain restricted classes and should not be tested
*
* @var array
*/
private $fixtureFiles = [];

/**
* Restricted classes
*
* @var array
*/
private $classes = [];

/**
* RestrictedCodeSniff constructor.
*/
public function __construct()
{
$this->loadData('restricted_classes*.php');
}

/**
* @inheritdoc
*/
public function register()
{
return [
T_STRING,
T_CONSTANT_ENCAPSED_STRING
];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
// phpcs:ignore
if (array_key_exists(basename($phpcsFile->getFilename()), $this->fixtureFiles)) {
return;
}

$tokens = $phpcsFile->getTokens();
$token = $tokens[$stackPtr]['content'];
if (array_key_exists($token, $this->classes)) {
if ($this->isExcluded($token, $phpcsFile)) {
return;
}
$phpcsFile->addError(
sprintf(
self::ERROR_MESSAGE,
$token,
$phpcsFile->getFilename(),
$this->classes[$token]['replacement']
),
$stackPtr,
self::ERROR_CODE,
);
}
}

/**
* Checks if currently parsed file should be excluded from analysis
*
* @param string $token
* @param File $phpcsFile
* @return bool
*/
private function isExcluded(string $token, File $phpcsFile): bool
{
if (in_array($phpcsFile->getFilename(), $this->fixtureFiles)) {
return true;
}
foreach ($this->classes[$token]['exclude'] as $exclude) {
if (strpos($phpcsFile->getFilename(), $exclude) !== false) {
return true;
}
}
return false;
}

/**
* Loads and merges data from fixtures
*
* @param string $filePattern
* @return void
*/
private function loadData(string $filePattern)
{
// phpcs:ignore
foreach (glob(__DIR__ . '/_files/' . $filePattern) as $file) {
$relativePath = str_replace(
'\\',
'/',
str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file)
);
array_push($this->fixtureFiles, $relativePath);
$this->classes = array_merge_recursive($this->classes, $this->readList($file));
}
}

/**
* Isolate including a file into a method to reduce scope
*
* @param string $file
* @return array
*/
private function readList($file)
{
// phpcs:ignore
return include $file;
}
}
Loading