ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,18 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\CodingStandard\Tool;
interface BlacklistInterface
{
/**
* Set list of paths to be excluded from tool run
*
* @param array $blackList
* @return void
*/
public function setBlackList(array $blackList);
}
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* PHP Code Mess v1.3.3 tool wrapper
*/
namespace Magento\TestFramework\CodingStandard\Tool;
use \Magento\TestFramework\CodingStandard\ToolInterface;
class CodeMessDetector implements ToolInterface
{
/**
* Ruleset directory
*
* @var string
*/
private $rulesetFile;
/**
* Report file
*
* @var string
*/
private $reportFile;
/**
* Constructor
*
* @param string $rulesetDir \Directory that locates the inspection rules
* @param string $reportFile Destination file to write inspection report to
*/
public function __construct($rulesetFile, $reportFile)
{
$this->reportFile = $reportFile;
$this->rulesetFile = $rulesetFile;
}
/**
* Whether the tool can be ran on the current environment
*
* @return bool
*/
public function canRun()
{
return class_exists(\PHPMD\TextUI\Command::class);
}
/**
* {@inheritdoc}
*/
public function run(array $whiteList)
{
if (empty($whiteList)) {
return \PHPMD\TextUI\Command::EXIT_SUCCESS;
}
$commandLineArguments = [
'run_file_mock', //emulate script name in console arguments
implode(',', $whiteList),
'text', //report format
$this->rulesetFile,
'--reportfile',
$this->reportFile,
];
$options = new \PHPMD\TextUI\CommandLineOptions($commandLineArguments);
$command = new \PHPMD\TextUI\Command();
return $command->run($options, new \PHPMD\RuleSetFactory());
}
}
@@ -0,0 +1,112 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\CodingStandard\Tool;
use Magento\TestFramework\CodingStandard\Tool\CodeSniffer\Wrapper;
use Magento\TestFramework\CodingStandard\ToolInterface;
/**
* PHP Code Sniffer tool wrapper
*/
class CodeSniffer implements ToolInterface, ExtensionInterface
{
/**
* Ruleset directory
*
* @var string
*/
private $rulesetDir;
/**
* Report file
*
* @var string
*/
private $reportFile;
/**
* PHPCS cli tool wrapper
*
* @var Wrapper
*/
private $wrapper;
/**
* List of extensions for tool run
*
* @var array
*/
private $extensions = [
'php' => 'PHP',
'phtml' => 'PHP',
];
/**
* Constructor
*
* @param string $rulesetDir \Directory that locates the inspection rules
* @param string $reportFile Destination file to write inspection report to
* @param Wrapper $wrapper
*/
public function __construct($rulesetDir, $reportFile, Wrapper $wrapper)
{
$this->rulesetDir = $rulesetDir;
// phpcs:ignore Magento2.Functions.DiscouragedFunction
if (!file_exists($rulesetDir) && file_exists($fullPath = realpath(__DIR__ . '/../../../../' . $rulesetDir))) {
$this->rulesetDir = $fullPath;
}
$this->reportFile = $reportFile;
$this->wrapper = $wrapper;
}
/**
* @inheritdoc
*/
public function setExtensions(array $extensions)
{
$this->extensions = $extensions;
}
/**
* Whether the tool can be ran on the current environment
*
* @return bool
*/
public function canRun()
{
return class_exists('\PHP_CodeSniffer\Runner');
}
/**
* @inheritdoc
*/
public function run(array $whiteList)
{
if (empty($whiteList)) {
return 0;
}
if (!defined('PHP_CODESNIFFER_IN_TESTS')) {
define('PHP_CODESNIFFER_IN_TESTS', true);
}
$this->wrapper->checkRequirements();
$settings = [];
$settings['files'] = $whiteList;
$settings['standards'] = [$this->rulesetDir];
$settings['extensions'] = $this->extensions;
$settings['reports']['full'] = $this->reportFile;
$settings['reportWidth'] = 120;
$this->wrapper->setSettings($settings);
// phpcs:ignore Magento2.Functions.DiscouragedFunction
ob_start();
$result = $this->wrapper->runPHPCS();
ob_end_clean();
return $result;
}
}
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* PHP Code Sniffer Cli wrapper
*/
namespace Magento\TestFramework\CodingStandard\Tool\CodeSniffer;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Runner;
/**
* PHP Code Sniffer wrapper class
*/
class Wrapper extends Runner
{
/**
* @var array
*/
private $settings = [];
/**
* Return the current version of php code sniffer
*
* @return string
*/
public function version()
{
$version = '0.0.0';
if (defined('\PHP_CodeSniffer\Config::VERSION')) {
$version = Config::VERSION;
}
return $version;
}
/**
* Initialize PHPCS runner and modifies the configuration settings
*
* @throws \PHP_CodeSniffer\Exceptions\DeepExitException
*/
public function init()
{
$this->config->extensions = $this->settings['extensions'];
unset($this->settings['extensions']);
$settings = $this->config->getSettings();
unset($settings['files']);
$this->config->setSettings($settings);
$this->config->setSettings(array_replace_recursive(
$this->config->getSettings(),
$this->settings
));
return parent::init();
}
/**
* Sets the settings
*
* @param array $settings
*/
public function setSettings($settings)
{
$this->settings = $settings;
}
}
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\CodingStandard\Tool;
/**
* Defines an interface for compatibility checks against a specific version.
*/
interface CompatibilityInterface
{
/**
* Sets the version against which to test code.
*
* @param string $version
* @return void
*/
public function setTestVersion(string $version): void;
}
@@ -0,0 +1,174 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\CodingStandard\Tool;
use Magento\TestFramework\CodingStandard\ToolInterface;
use SebastianBergmann\FileIterator\Facade;
use SebastianBergmann\PHPCPD\Detector\Detector;
use SebastianBergmann\PHPCPD\Detector\Strategy\DefaultStrategy;
use SebastianBergmann\PHPCPD\Log\PMD;
use SebastianBergmann\PHPCPD\Log\Text;
use Symfony\Component\Finder\Finder;
/**
* PHP Copy Paste Detector tool wrapper
*/
class CopyPasteDetector implements ToolInterface, BlacklistInterface
{
/**
* Minimum number of equal lines to identify a copy paste snippet
*/
private const MIN_LINES = 13;
/**
* Destination file to write inspection report to
*
* @var string
*/
private $reportFile;
/**
* List of paths to be excluded from tool run
*
* @var array
*/
private $blacklist;
/**
* @param string $reportFile
*/
public function __construct(string $reportFile)
{
$this->reportFile = $reportFile;
}
/**
* @inheritdoc
*/
public function setBlackList(array $blackList): void
{
$this->blacklist = $blackList;
}
/**
* Whether the tool can be run in the current environment
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*
* @return bool
*/
public function canRun(): bool
{
return class_exists(Detector::class)
&& class_exists(Facade::class)
&& class_exists(Finder::class);
}
/**
* Run tool for files specified
*
* @param array $whiteList Files/directories to be inspected
* @return bool
*/
public function run(array $whiteList): bool
{
$clones = (new Detector(new DefaultStrategy()))->copyPasteDetection(
(new Facade())->getFilesAsArray(
$whiteList,
'',
'',
$this->getExclude()
),
self::MIN_LINES
);
(new PMD($this->reportFile))->processClones($clones);
(new Text)->printResult($clones, false);
return count($clones) === 0;
}
/**
* Get exclude params from blacklist
*
* @return string[]
*/
private function getExclude(): array
{
$exclude = [];
$blacklistedDirs = [];
$blacklistedFileNames = [];
$blacklistedPatterns = [];
foreach ($this->blacklist as $file) {
$file = trim($file);
if (!$file) {
continue;
}
$realPath = realpath(BP . '/' . $file);
if ($realPath === false) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($ext != '') {
$blacklistedFileNames[] = $file;
} else {
$blacklistedPatterns[] = $file;
}
continue;
}
$exclude[] = [$realPath];
$blacklistedDirs[] = $file;
}
foreach ($blacklistedPatterns as $pattern) {
$files = $this->find($pattern, false, $blacklistedDirs);
if (empty($files)) {
continue;
}
$exclude[] = $files;
}
foreach ($blacklistedFileNames as $fileName) {
$files = $this->find($fileName, true, $blacklistedDirs);
if (empty($files)) {
continue;
}
$exclude[] = $files;
}
return array_unique(array_merge(...$exclude));
}
/**
* Find all files by pattern
*
* @param string $pattern
* @param bool $searchFiles
* @param array $excludePaths
* @return array
*/
private function find(string $pattern, bool $searchFiles, array $excludePaths): array
{
$finder = new Finder();
$finder->in(BP);
$finder->notPath($excludePaths);
if ($searchFiles) {
$finder->files();
$finder->name($pattern);
} else {
$finder->path($pattern);
}
$result = [];
foreach ($finder as $file) {
$result[] = $file->getRealPath();
}
return $result;
}
}
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\CodingStandard\Tool;
interface ExtensionInterface
{
/**
* Set extensions for tool to run
* Example: 'php', 'xml', 'phtml', 'css'
*
* @param array $extensions
* @return void
*/
public function setExtensions(array $extensions);
}
@@ -0,0 +1,108 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\CodingStandard\Tool;
use Magento\TestFramework\CodingStandard\ToolInterface;
/**
* PhpStan tool wrapper.
*/
class PhpStan implements ToolInterface
{
/**
* Rule level to be used.
*
* @see https://github.com/phpstan/phpstan#rule-levels
*/
private const RULE_LEVEL = 1;
/**
* Memory limit required by PHPStan for full Magento project scan.
*/
private const MEMORY_LIMIT = '4G';
/**
* Error formatter to be used.
*
* @see https://github.com/phpstan/phpstan#existing-error-formatters-to-be-used
*/
private const ERROR_FORMAT = 'filtered';
/**
* Report file.
*
* @var string
*/
private $reportFile;
/**
* PHPStan configuration file in neon format.
*
* @var string
*/
private $confFile;
/**
* @param string $confFile
* @param string $reportFile
*/
public function __construct(string $confFile, string $reportFile)
{
$this->reportFile = $reportFile;
$this->confFile = $confFile;
}
/**
* @inheritdoc
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function canRun(): bool
{
// phpcs:disable Magento2.Security.InsecureFunction
exec($this->getCommand() . ' --version', $output, $exitCode);
return $exitCode === 0;
}
/**
* @inheritdoc
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function run(array $whiteList): int
{
if (empty($whiteList)) {
return 0;
}
$command = $this->getCommand() . ' analyse' .
' --level ' . self::RULE_LEVEL .
' --no-progress' .
' --error-format=' . self::ERROR_FORMAT .
' --memory-limit=' . self::MEMORY_LIMIT .
// phpcs:ignore Magento2.Functions.DiscouragedFunction
' --configuration ' . escapeshellarg($this->confFile) .
' ' . implode(' ', $whiteList) .
' > ' . $this->reportFile;
// phpcs:disable Magento2.Security.InsecureFunction
exec($command, $output, $exitCode);
return $exitCode;
}
/**
* Get PHPStan CLI command
*
* @return string
*/
private function getCommand(): string
{
// phpcs:ignore Magento2.Security.IncludeFile
$vendorDir = require BP . '/app/etc/vendor_path.php';
return 'php ' . BP . '/' . $vendorDir . '/bin/phpstan';
}
}
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard tool wrapper interface
*/
namespace Magento\TestFramework\CodingStandard;
interface ToolInterface
{
/**
* Whether the tool can be ran on the current environment
*
* @return bool
*/
public function canRun();
/**
* Run tool for files specified
*
* @param array $whiteList Files/directories to be inspected
* @return int
*/
public function run(array $whiteList);
}
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Dependency;
/**
* Class provides dependency rule for analytics.xml config file.
*/
class AnalyticsConfigRule implements RuleInterface
{
/**
* @inheritdoc
*/
public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
{
if ('config' != $fileType || !preg_match('#.*/analytics\.xml$#', $file)) {
return [];
}
$dependenciesInfo = [];
if (preg_match_all('#<[customProvider|reportProvider][^>]*class=[\'"]([^\'"]+)[\'"]#i', $contents, $matches)) {
$classes = array_pop($matches);
foreach ($classes as $class) {
$classParts = explode('\\', $class);
$module = implode('\\', array_slice($classParts, 0, 2));
if (strtolower($currentModule) !== strtolower($module)) {
$dependenciesInfo[] = [
'modules' => [$module],
'type' => RuleInterface::TYPE_HARD,
'source' => $file,
];
}
}
}
return $dependenciesInfo;
}
}
@@ -0,0 +1,72 @@
<?php
/**
* Rule for searching DB dependency
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Dependency;
/**
* Class to get DB dependencies information
*/
class DbRule implements \Magento\TestFramework\Dependency\RuleInterface
{
/**
* Map of tables and modules
*
* @var array
*/
protected $_moduleTableMap;
/**
* Constructor
*
* @param array $tables
*/
public function __construct(array $tables)
{
$this->_moduleTableMap = $tables;
}
/**
* Gets alien dependencies information for current module by analyzing file's contents
*
* @param string $currentModule
* @param string $fileType
* @param string $file
* @param string $contents
* @return array
*/
public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
{
if ('php' !== $fileType || !preg_match('#.*/(Setup|Resource|Query)/.*\.php$#', $file)) {
return [];
}
$dependenciesInfo = [];
$unKnowTables = [];
if (preg_match_all('#>gettable(name)?\([\'"]([^\'"]+)[\'"]\)#i', $contents, $matches)) {
$tables = array_pop($matches);
foreach ($tables as $table) {
if (!isset($this->_moduleTableMap[$table])) {
$unKnowTables[$file][$table] = $table;
continue;
}
if (strtolower($currentModule) !== strtolower($this->_moduleTableMap[$table])) {
$dependenciesInfo[] = [
'modules' => [$this->_moduleTableMap[$table]],
'type' => \Magento\TestFramework\Dependency\RuleInterface::TYPE_HARD,
'source' => $table,
];
}
}
}
foreach ($unKnowTables as $tables) {
foreach ($tables as $table) {
$dependenciesInfo[] = ['modules' => ['Unknown'], 'source' => $table];
}
}
return $dependenciesInfo;
}
}
@@ -0,0 +1,166 @@
<?php
/**
* Rule for searching php file dependency
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Dependency;
use Magento\Framework\App\Utility\Classes;
use Magento\Framework\App\Utility\Files;
use Magento\TestFramework\Dependency\VirtualType\VirtualTypeMapper;
/**
* Class provide dependency rule for di.xml config files.
*/
class DiRule implements RuleInterface
{
/**
* @var VirtualTypeMapper
*/
private $mapper;
/**
* @var string
*/
private $pattern;
/**
* @param VirtualTypeMapper $mapper
*/
public function __construct(VirtualTypeMapper $mapper)
{
$this->mapper = $mapper;
}
/**
* Get class name pattern.
*
* @return string
* @throws \Exception
*/
private function getPattern()
{
if ($this->pattern === null) {
$this->pattern = '~\b(?<class>(?<module>('
. implode('[_\\\\]|', Files::init()->getNamespaces())
. '[_\\\\])[a-zA-Z0-9]+)[a-zA-Z0-9_\\\\]*)\b~';
}
return $this->pattern;
}
/**
* @var array
*/
private static $tagNameMap = [
'type' => ['name'],
'preference' => [
'type',
'for'
],
'plugin' => ['type'],
'virtualType' => ['type']
];
/**
* Gets alien dependencies information for current module by analyzing file's contents
*
* @param string $currentModule
* @param string $fileType
* @param string $file
* @param string $contents
* @return array
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
{
//phpcs:ignore Magento2.Functions.DiscouragedFunction
if (pathinfo($file, PATHINFO_BASENAME) !== 'di.xml') {
return [];
}
$dependenciesInfo = [];
$scope = $this->mapper->getScopeFromFile($file);
foreach ($this->fetchPossibleDependencies($contents) as $type => $deps) {
foreach ($deps as $dep) {
$dep = $this->mapper->getType($dep, $scope);
if (preg_match($this->getPattern(), $dep, $matches)) {
$referenceModule = str_replace('_', '\\', $matches['module']);
if ($currentModule === $referenceModule) {
continue;
}
$dependenciesInfo[] = [
'modules' => [$referenceModule],
'type' => $type,
'source' => $matches['class'],
];
}
}
}
return $dependenciesInfo;
}
/**
* Fetch all possible dependencies.
*
* @param string $contents
* @return array
*/
private function fetchPossibleDependencies($contents)
{
$doc = new \DOMDocument();
$doc->loadXML($contents);
return [
RuleInterface::TYPE_SOFT => $this->getSoftDependencies($doc),
RuleInterface::TYPE_HARD => $this->getHardDependencies($doc)
];
}
/**
* Collect soft dependencies.
*
* @param \DOMDocument $doc
* @return array
*/
private function getSoftDependencies(\DOMDocument $doc)
{
$result = [];
foreach (self::$tagNameMap as $tagName => $attributeNames) {
$nodes = $doc->getElementsByTagName($tagName);
/** @var \DOMElement $node */
foreach ($nodes as $node) {
if ($tagName === 'virtualType' && !$node->getAttribute('type')) {
$result[] = Classes::resolveVirtualType($node->getAttribute('name'));
continue;
}
foreach ($attributeNames as $attributeName) {
$result[] = $node->getAttribute($attributeName);
}
}
}
return $result;
}
/**
* Collect hard dependencies.
*
* @param \DOMDocument $doc
* @return array
*/
private function getHardDependencies(\DOMDocument $doc)
{
$result = [];
$xpath = new \DOMXPath($doc);
$textNodes = $xpath->query('//*[@xsi:type="object"]');
/** @var \DOMElement $node */
foreach ($textNodes as $node) {
$result[] = $node->nodeValue;
}
return $result;
}
}
@@ -0,0 +1,512 @@
<?php
/**
* Rule for searching dependencies in layout files
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Dependency;
class LayoutRule implements \Magento\TestFramework\Dependency\RuleInterface
{
/**
* Default modules list.
*
* @var array
*/
protected $_defaultModules = [
'frontend' => 'Magento\Theme',
'adminhtml' => 'Magento\Adminhtml',
];
/**
* Namespaces to analyze
*
* Format: {Namespace}|{Namespace}|...
*
* @var string
*/
protected $_namespaces;
/**
* List of routers
*
* Format: array(
* '{Router}' => '{Module_Name}'
* )
*
* @var array
*/
protected $_mapRouters = [];
/**
* List of layout blocks
*
* Format: array(
* '{Area}' => array(
* '{Block_Name}' => array('{Module_Name}' => '{Module_Name}')
* ))
*
* @var array
*/
protected $_mapLayoutBlocks = [];
/**
* List of layout handles
*
* Format: array(
* '{Area}' => array(
* '{Handle_Name}' => array('{Module_Name}' => '{Module_Name}')
* ))
*
* @var array
*/
protected $_mapLayoutHandles = [];
/**
* Unknown layout handle
*/
const EXCEPTION_TYPE_UNKNOWN_HANDLE = 'UNKNOWN_HANDLE';
/**
* Unknown layout block
*/
const EXCEPTION_TYPE_UNKNOWN_BLOCK = 'UNKNOWN_BLOCK';
/**
* Undefined dependency
*/
const EXCEPTION_TYPE_UNDEFINED_DEPENDENCY = 'UNDEFINED_DEPENDENCY';
/**
* Constructor
*
* @param array $mapRouters
* @param array $mapLayoutBlocks
* @param array $mapLayoutHandles
*/
public function __construct(array $mapRouters, array $mapLayoutBlocks, array $mapLayoutHandles)
{
$this->_mapRouters = $mapRouters;
$this->_mapLayoutBlocks = $mapLayoutBlocks;
$this->_mapLayoutHandles = $mapLayoutHandles;
$this->_namespaces = implode('|', \Magento\Framework\App\Utility\Files::init()->getNamespaces());
}
/**
* Retrieve dependencies information for current module
*
* @param string $currentModule
* @param string $fileType
* @param string $file
* @param string $contents
* @return array
*/
public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
{
if ('layout' != $fileType) {
return [];
}
$attributes = $this->_caseAttributeModule($currentModule, $contents);
$blocks = $this->_caseElementBlock($currentModule, $contents);
$actions = $this->_caseElementAction($currentModule, $contents);
$handle = $this->_caseLayoutHandle($currentModule, $file, $contents);
$handleParents = $this->_caseLayoutHandleParent($currentModule, $file, $contents);
$handleUpdates = $this->_caseLayoutHandleUpdate($currentModule, $file, $contents);
$references = $this->_caseLayoutReference($currentModule, $file, $contents);
$dependencies = array_merge(
$attributes,
$blocks,
$actions,
$handle,
$handleParents,
$handleUpdates,
$references
);
return $dependencies;
}
/**
* Check dependencies for 'module' attribute
*
* Ex.: <element module="{module}">
*
* @param string $currentModule
* @param string $contents
* @return array
*/
protected function _caseAttributeModule($currentModule, &$contents)
{
$patterns = [
'/(?<source><.+module\s*=\s*[\'"](?<namespace>' .
$this->_namespaces .
')[_\\\\]' .
'(?<module>[A-Z][a-zA-Z]+)[\'"].*>)/' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
];
return $this->_checkDependenciesByRegexp($currentModule, $contents, $patterns);
}
/**
* Check dependencies for <block> element
*
* Ex.: <block class="{name}">
* <block template="{path}">
*
* @param string $currentModule
* @param string $contents
* @return array
*/
protected function _caseElementBlock($currentModule, &$contents)
{
$patterns = [
'/(?<source><block.*class\s*=\s*[\'"](?<namespace>' .
$this->_namespaces .
')[_\\\\]' .
'(?<module>[A-Z][a-zA-Z]+)[_\\\\]' .
'(?:[A-Z][a-zA-Z]+[_\\\\]?){1,}[\'"].*>)/' => \Magento\Test\Integrity\DependencyTest::TYPE_HARD,
'/(?<source><block.*template\s*=\s*[\'"](?<namespace>' .
$this->_namespaces .
')[_\\\\]' .
'(?<module>[A-Z][a-zA-Z]+)::[\w\/\.]+[\'"].*>)/' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
];
return $this->_checkDependenciesByRegexp($currentModule, $contents, $patterns);
}
/**
* Check dependencies for <action> element
*
* Ex.: <block>{name}
* <template>{path}
* <file>{path}
* <element helper="{name}">
*
* @param string $currentModule
* @param string $contents
* @return array
*/
protected function _caseElementAction($currentModule, &$contents)
{
$patterns = [
'/(?<source><block\s*>(?<namespace>' .
$this->_namespaces .
')[_\\\\]' .
'(?<module>[A-Z][a-zA-Z]+)[_\\\\]' .
'(?:[A-Z][a-zA-Z]+[_\\\\]?){1,}<\/block\s*>)/' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
'/(?<source><template\s*>(?<namespace>' .
$this->_namespaces .
')[_\\\\]' .
'(?<module>[A-Z][a-zA-Z]+)::[\w\/\.]+' .
'<\/template\s*>)/' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
'/(?<source><file\s*>(?<namespace>' .
$this->_namespaces .
')[_\\\\]' .
'(?<module>[A-Z][a-zA-Z]+)::[\w\/\.-]+' .
'<\/file\s*>)/' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
'/(?<source><.*helper\s*=\s*[\'"](?<namespace>' .
$this->_namespaces .
')[_\\\\]' .
'(?<module>[A-Z][a-zA-Z]+)[_\\\\](?:[A-Z][a-z]+[_\\\\]?){1,}::[\w]+' .
'[\'"].*>)/' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
];
return $this->_checkDependenciesByRegexp($currentModule, $contents, $patterns);
}
/**
* Check layout handles
*
* Ex.: <layout><{name}>...</layout>
*
* @param string $currentModule
* @param string $file
* @param string $contents
* @return array
*/
protected function _caseLayoutHandle($currentModule, $file, &$contents)
{
$xml = simplexml_load_string($contents);
if (!$xml) {
return [];
}
$area = $this->_getAreaByFile($file);
$result = [];
foreach ((array)$xml->xpath('/layout/child::*') as $element) {
$check = $this->_checkDependencyLayoutHandle($currentModule, $area, $element->getName());
$modules = isset($check['modules']) ? $check['modules'] : null;
if ($modules) {
foreach ($modules as $module) {
$result[$module] = [
'type' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
'source' => $element->getName(),
];
}
}
}
return $this->_getUniqueDependencies($result);
}
/**
* Check layout handles parents
*
* Ex.: <layout_name parent="{name}">
*
* @param string $currentModule
* @param string $file
* @param string $contents
* @return array
*/
protected function _caseLayoutHandleParent($currentModule, $file, &$contents)
{
$xml = simplexml_load_string($contents);
if (!$xml) {
return [];
}
$area = $this->_getAreaByFile($file);
$result = [];
foreach ((array)$xml->xpath('/layout/child::*/@parent') as $element) {
$check = $this->_checkDependencyLayoutHandle($currentModule, $area, (string)$element);
$modules = isset($check['modules']) ? $check['modules'] : null;
if ($modules) {
foreach ($modules as $module) {
$result[$module] = [
'type' => \Magento\Test\Integrity\DependencyTest::TYPE_HARD,
'source' => (string)$element,
];
}
}
}
return $this->_getUniqueDependencies($result);
}
/**
* Check layout handles updates
*
* Ex.: <update handle="{name}" />
*
* @param string $currentModule
* @param string $file
* @param string $contents
* @return array
*/
protected function _caseLayoutHandleUpdate($currentModule, $file, &$contents)
{
$xml = simplexml_load_string($contents);
if (!$xml) {
return [];
}
$area = $this->_getAreaByFile($file);
$result = [];
foreach ((array)$xml->xpath('//update/@handle') as $element) {
$check = $this->_checkDependencyLayoutHandle($currentModule, $area, (string)$element);
$modules = isset($check['modules']) ? $check['modules'] : null;
if ($modules) {
foreach ($modules as $module) {
$result[$module] = [
'type' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
'source' => (string)$element,
];
}
}
}
return $this->_getUniqueDependencies($result);
}
/**
* Check layout references
*
* Ex.: <reference name="{name}">
*
* @param string $currentModule
* @param string $file
* @param string $contents
* @return array
*/
protected function _caseLayoutReference($currentModule, $file, &$contents)
{
$xml = simplexml_load_string($contents);
if (!$xml) {
return [];
}
$area = $this->_getAreaByFile($file);
$result = [];
foreach ((array)$xml->xpath('//reference/@name') as $element) {
$check = $this->_checkDependencyLayoutBlock($currentModule, $area, (string)$element);
$modules = isset($check['modules']) ? $check['modules'] : null;
if ($modules) {
foreach ($modules as $module) {
$result[$module] = [
'type' => \Magento\Test\Integrity\DependencyTest::TYPE_SOFT,
'source' => (string)$element,
];
}
}
}
return $this->_getUniqueDependencies($result);
}
/**
* Search dependencies by defined regexp patterns
*
* @param string $currentModule
* @param string $contents
* @param array $patterns
* @return array
*/
protected function _checkDependenciesByRegexp($currentModule, &$contents, $patterns = [])
{
$result = [];
foreach ($patterns as $pattern => $type) {
if (preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$module = $match['namespace'] . '\\' . $match['module'];
if ($currentModule != $module) {
$result[$module] = ['type' => $type, 'source' => $match['source']];
}
}
}
}
return $this->_getUniqueDependencies($result);
}
/**
* Check layout handle dependency
*
* Return: array(
* 'modules' // dependent modules
* 'source' // source text
* )
*
* @param admin $currentModule
* @param string $area
* @param string $handle
* @return string[]
*/
protected function _checkDependencyLayoutHandle($currentModule, $area, $handle)
{
$chunks = explode('_', $handle);
if (count($chunks) > 1) {
// Remove 'action' part from handle name
array_pop($chunks);
}
$router = implode('_', $chunks);
if (isset($this->_mapRouters[$router])) {
// CASE 1: Single dependency
$modules = $this->_mapRouters[$router];
if (!in_array($currentModule, $modules)) {
return ['modules' => $modules];
}
}
if (isset($this->_mapLayoutHandles[$area][$handle])) {
// CASE 2: No dependencies
$modules = $this->_mapLayoutHandles[$area][$handle];
if (isset($modules[$currentModule])) {
return ['modules' => []];
}
// CASE 3: Single dependency
if (1 == count($modules)) {
return ['modules' => $modules];
}
// CASE 4: Default module dependency
$defaultModule = $this->_getDefaultModuleName($area);
if (isset($modules[$defaultModule])) {
return ['modules' => [$defaultModule]];
}
}
return [];
}
/**
* Check layout block dependency
*
* Return: array(
* 'modules' // dependent modules
* 'source' // source text
* )
*
* @param string $currentModule
* @param string $area
* @param string $block
* @return string[]
*/
protected function _checkDependencyLayoutBlock($currentModule, $area, $block)
{
if (isset($this->_mapLayoutBlocks[$area][$block])) {
// CASE 1: No dependencies
$modules = $this->_mapLayoutBlocks[$area][$block];
if (isset($modules[$currentModule])) {
return ['modules' => []];
}
// CASE 2: Single dependency
if (1 == count($modules)) {
return ['modules' => $modules];
}
// CASE 3: Default module dependency
$defaultModule = $this->_getDefaultModuleName($area);
if (isset($modules[$defaultModule])) {
return ['modules' => [$defaultModule]];
}
}
return [];
}
/**
* Get area from file path
*
* @param string $file
* @return string
*/
protected function _getAreaByFile($file)
{
$area = 'default';
if (preg_match('/\/(?<area>adminhtml|frontend)\//', $file, $matches)) {
$area = $matches['area'];
}
return $area;
}
/**
* Retrieve unique dependencies
*
* @param array $dependencies
* @return array
*/
protected function _getUniqueDependencies($dependencies = [])
{
$result = [];
foreach ($dependencies as $module => $value) {
$result[] = ['modules' => [$module], 'type' => $value['type'], 'source' => $value['source']];
}
return $result;
}
/**
* Retrieve default module name (by area)
*
* @param string $area
* @return string|null
*/
protected function _getDefaultModuleName($area = 'default')
{
if (isset($this->_defaultModules[$area])) {
return $this->_defaultModules[$area];
}
return null;
}
}
@@ -0,0 +1,614 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Dependency;
use Magento\Framework\App\Utility\Files;
use Magento\Framework\Config\Reader\Filesystem as ConfigReader;
use Magento\Framework\Exception\ConfigurationMismatchException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\UrlInterface;
use Magento\TestFramework\Dependency\Reader\ClassScanner;
use Magento\TestFramework\Dependency\Route\RouteMapper;
use Magento\TestFramework\Exception\NoSuchActionException;
use Magento\TestFramework\Inspection\Exception;
/**
* Rule to check the dependencies between modules based on references, getUrl and layout blocks
*/
class PhpRule implements RuleInterface
{
/**
* List of filepaths for DI files
*
* @var array
*/
private $diFiles;
/**
* Map from plugin classes to the subjects they modify
*
* @var array
*/
private $pluginMap;
/**
* List of routers
*
* Format: array(
* '{Router}' => '{Module_Name}'
* )
*
* @var array
*/
private $_mapRouters = [];
/**
* List of layout blocks
*
* Format: array(
* '{Area}' => array(
* '{Block_Name}' => array('{Module_Name}' => '{Module_Name}')
* ))
*
* @var array
*/
protected $_mapLayoutBlocks = [];
/**
* Used to retrieve information from WebApi urls
* @var ConfigReader
*/
protected $configReader;
/**
* Default modules list.
*
* @var array
*/
protected $_defaultModules = [
'frontend' => 'Magento\Theme',
'adminhtml' => 'Magento\Adminhtml',
];
/**
* @var RouteMapper
*/
private $routeMapper;
/**
* Whitelists for dependency check
*
* @var array
*/
private $whitelists;
/**
* @var ClassScanner
*/
private $classScanner;
/**
* @var array
*/
private $serviceMethods;
/**
* @param array $mapRouters
* @param array $mapLayoutBlocks
* @param ConfigReader $configReader
* @param array $pluginMap
* @param array $whitelists
* @param ClassScanner|null $classScanner
* @param RouteMapper|null $routeMapper
*/
public function __construct(
array $mapRouters,
array $mapLayoutBlocks,
ConfigReader $configReader,
array $pluginMap = [],
array $whitelists = [],
ClassScanner $classScanner = null,
RouteMapper $routeMapper = null
) {
$this->_mapRouters = $mapRouters;
$this->_mapLayoutBlocks = $mapLayoutBlocks;
$this->configReader = $configReader;
$this->pluginMap = $pluginMap ?: null;
$this->whitelists = $whitelists;
$this->classScanner = $classScanner ?? new ClassScanner();
$this->routeMapper = $routeMapper ?? new RouteMapper();
}
/**
* Gets alien dependencies information for current module by analyzing file's contents
*
* @param string $currentModule
* @param string $fileType
* @param string $file
* @param string $contents
* @return array
* @throws \Exception
*/
public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
{
if (!in_array($fileType, ['php', 'template', 'fixture'])) {
return [];
}
$dependenciesInfo = [];
$dependenciesInfo = $this->considerCaseDependencies(
$dependenciesInfo,
$this->caseClassesAndIdentifiers($currentModule, $file, $contents)
);
$dependenciesInfo = $this->considerCaseDependencies(
$dependenciesInfo,
$this->_caseGetUrl($currentModule, $contents, $file)
);
$dependenciesInfo = $this->considerCaseDependencies(
$dependenciesInfo,
$this->_caseLayoutBlock($currentModule, $fileType, $file, $contents)
);
return $dependenciesInfo;
}
/**
* Get routes whitelist
*
* @return array
*/
private function getRoutesWhitelist(): array
{
return $this->whitelists['routes'] ?? [];
}
/**
* Check references to classes and identifiers defined in other modules
*
* @param string $currentModule
* @param string $file
* @param string $contents
* @return array
* @throws \Exception
*/
private function caseClassesAndIdentifiers($currentModule, $file, &$contents)
{
$pattern = '~\b(?<class>(?<module>('
. implode(
'[_\\\\]|',
Files::init()->getNamespaces()
)
. '(?<delimiter>[_\\\\]))[a-zA-Z0-9]{2,})'
. '(?<class_inside_module>\\4[a-zA-Z0-9_\\\\]{2,})?)\b'
. '(?:::(?<module_scoped_key>[A-Za-z0-9_/.]+)[\'"])?~';
if (!preg_match_all($pattern, $contents, $matches)) {
return [];
}
$dependenciesInfo = [];
$matches['module'] = array_unique($matches['module']);
foreach ($matches['module'] as $i => $referenceModule) {
$referenceModule = str_replace('_', '\\', $referenceModule);
if ($currentModule == $referenceModule) {
continue;
}
$dependencyClass = trim($matches['class'][$i]);
if (empty($matches['class_inside_module'][$i]) && !empty($matches['module_scoped_key'][$i])) {
$dependencyType = RuleInterface::TYPE_SOFT;
} else {
$currentClass = $this->getClassFromFilepath($file);
$dependencyType = $this->isPluginDependency($currentClass, $dependencyClass)
? RuleInterface::TYPE_SOFT
: RuleInterface::TYPE_HARD;
}
$dependenciesInfo[] = [
'modules' => [$referenceModule],
'type' => $dependencyType,
'source' => $dependencyClass,
];
}
return $dependenciesInfo;
}
/**
* Get class name from filename based on class/file naming conventions
*
* @param string $filepath
* @return string
*/
private function getClassFromFilepath(string $filepath): string
{
return $this->classScanner->getClassName($filepath);
}
/**
* Load DI configuration files
*
* @return array
* @throws \Exception
*/
private function loadDiFiles()
{
if (!$this->diFiles) {
$this->diFiles = Files::init()->getDiConfigs();
}
return $this->diFiles;
}
/**
* Generate an array of plugin info
*
* @return array
* @throws \Exception
*/
private function loadPluginMap()
{
if (!$this->pluginMap) {
foreach ($this->loadDiFiles() as $filepath) {
$dom = new \DOMDocument();
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$dom->loadXML(file_get_contents($filepath));
$typeNodes = $dom->getElementsByTagName('type');
/** @var \DOMElement $type */
foreach ($typeNodes as $type) {
/** @var \DOMElement $plugin */
foreach ($type->getElementsByTagName('plugin') as $plugin) {
$subject = $type->getAttribute('name');
$pluginType = $plugin->getAttribute('type');
$this->pluginMap[$pluginType] = $subject;
}
}
}
}
return $this->pluginMap;
}
/**
* Determine whether a the dependency relation is because of a plugin
*
* True IFF the dependent is a plugin for some class in the same module as the dependency.
*
* @param string $dependent
* @param string $dependency
* @return bool
* @throws \Exception
*/
private function isPluginDependency($dependent, $dependency)
{
$pluginMap = $this->loadPluginMap();
$subject = isset($pluginMap[$dependent])
? $pluginMap[$dependent]
: null;
if ($subject === $dependency) {
return true;
} elseif ($subject) {
$moduleNameLength = strpos($subject, '\\', strpos($subject, '\\') + 1);
$subjectModule = substr($subject, 0, $moduleNameLength);
return strpos($dependency, $subjectModule) === 0;
} else {
return false;
}
}
/**
* Check get URL method
*
* Ex.: getUrl('{path}')
*
* @param string $currentModule
* @param string $contents
* @param string $file
* @return array
* @throws LocalizedException
*/
protected function _caseGetUrl(string $currentModule, string &$contents, string $file): array
{
$dependencies = [];
$pattern = '#(\->|:)(?<source>getUrl\(([\'"])(?<path>[a-zA-Z0-9\-_*/]+)\3)\s*[,)]#';
if (!preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER)) {
return $dependencies;
}
try {
foreach ($matches as $item) {
$path = $item['path'];
$modules = [];
if (strpos($path, '*') !== false) {
$modules = $this->processWildcardUrl($path, $file);
} elseif (preg_match('#rest(?<service>/V1/.+)#i', $path, $apiMatch)) {
$modules = $this->processApiUrl($apiMatch['service']);
} else {
$modules = $this->processStandardUrl($path);
}
if ($modules && !in_array($currentModule, $modules)) {
$dependencies[] = [
'modules' => $modules,
'type' => RuleInterface::TYPE_HARD,
'source' => $item['source'],
];
}
}
} catch (NoSuchActionException $e) {
if (array_search($e->getMessage(), $this->getRoutesWhitelist()) === false) {
throw new LocalizedException(__('Invalid URL path: %1', $e->getMessage()), $e);
}
}
return $dependencies;
}
/**
* Helper method to get module dependencies used by a wildcard Url
*
* @param string $urlPath
* @param string $filePath
* @return string[]
* @throws NoSuchActionException
*/
private function processWildcardUrl(string $urlPath, string $filePath)
{
$filePath = strtolower($filePath);
$urlRoutePieces = explode('/', $urlPath);
$routeId = array_shift($urlRoutePieces);
//Skip route wildcard processing as this requires using the routeMapper
if ('*' === $routeId) {
return [];
}
/**
* Only handle Controllers. ie: Ignore Blocks, Templates, and Models due to complexity in static resolution
* of route
*/
if (!preg_match(
'#controller/(adminhtml/)?(?<controller_name>.+)/(?<action_name>\w+).php$#',
$filePath,
$fileParts
)) {
return [];
}
$controllerName = array_shift($urlRoutePieces);
if ('*' === $controllerName) {
$controllerName = str_replace('/', '_', $fileParts['controller_name']);
}
if (empty($urlRoutePieces) || !$urlRoutePieces[0]) {
$actionName = UrlInterface::DEFAULT_ACTION_NAME;
} else {
$actionName = array_shift($urlRoutePieces);
if ('*' === $actionName) {
$actionName = $fileParts['action_name'];
}
}
return $this->routeMapper->getDependencyByRoutePath(
strtolower($routeId),
strtolower($controllerName),
strtolower($actionName)
);
}
/**
* Helper method to get module dependencies used by a standard URL
*
* @param string $path
* @return string[]
* @throws NoSuchActionException
*/
private function processStandardUrl(string $path)
{
$pattern = '#(?<route_id>[a-z0-9\-_]{3,})'
. '(/(?<controller_name>[a-z0-9\-_]+))?(/(?<action_name>[a-z0-9\-_]+))?#i';
if (!preg_match($pattern, $path, $match)) {
throw new NoSuchActionException('Failed to parse standard url path: ' . $path);
}
$routeId = $match['route_id'];
$controllerName = $match['controller_name'] ?? UrlInterface::DEFAULT_CONTROLLER_NAME;
$actionName = $match['action_name'] ?? UrlInterface::DEFAULT_ACTION_NAME;
return $this->routeMapper->getDependencyByRoutePath(
$routeId,
$controllerName,
$actionName
);
}
/**
* Create regex patterns from service url paths
*
* @return array
*/
private function getServiceMethodRegexps(): array
{
if (!$this->serviceMethods) {
$this->serviceMethods = [];
$serviceRoutes = $this->configReader->read()['routes'];
foreach ($serviceRoutes as $serviceRouteUrl => $methods) {
$pattern = '#:\w+#';
$replace = '\w+';
$serviceRouteUrlRegex = preg_replace($pattern, $replace, $serviceRouteUrl);
$serviceRouteUrlRegex = '#^' . $serviceRouteUrlRegex . '$#';
$this->serviceMethods[$serviceRouteUrlRegex] = $methods;
}
}
return $this->serviceMethods;
}
/**
* Helper method to get module dependencies used by an API URL
*
* @param string $path
* @return string[]
*
* @throws NoSuchActionException
* @throws Exception
*/
private function processApiUrl(string $path): array
{
foreach ($this->getServiceMethodRegexps() as $serviceRouteUrlRegex => $methods) {
/**
* Since we expect that every service method should be within the same module, we can use the class from
* any method
*/
if (preg_match($serviceRouteUrlRegex, $path)) {
$method = reset($methods);
$className = $method['service']['class'];
//get module from className
if (preg_match('#^(?<module>\w+[\\\]\w+)#', $className, $match)) {
return [$match['module']];
}
throw new Exception('Failed to parse class from className: ' . $className);
}
}
throw new NoSuchActionException('Failed to match service with url path: ' . $path);
}
/**
* Check layout blocks
*
* @param string $currentModule
* @param string $fileType
* @param string $file
* @param string $contents
* @return array
*/
protected function _caseLayoutBlock(string $currentModule, string $fileType, string $file, string &$contents): array
{
$pattern = '/[\->:]+(?<source>(?:getBlock|getBlockHtml)\([\'"](?<block>[\w\.\-]+)[\'"]\))/';
$area = $this->_getAreaByFile($file, $fileType);
$result = [];
if (!preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER)) {
return $result;
}
foreach ($matches as $match) {
if (in_array($match['block'], ['root', 'content'])) {
continue;
}
$check = $this->_checkDependencyLayoutBlock($currentModule, $area, $match['block']);
$modules = isset($check['modules']) ? $check['modules'] : null;
if ($modules) {
foreach ($modules as $module) {
$result[$module] = [
'type' => RuleInterface::TYPE_HARD,
'source' => $match['source'],
];
}
}
}
return $this->_getUniqueDependencies($result);
}
/**
* Get area from file path
*
* @param string $file
* @param string $fileType
* @return string|null
*/
protected function _getAreaByFile(string $file, string $fileType): ?string
{
if ($fileType == 'php') {
return null;
}
$area = 'default';
if (preg_match('/\/(?<area>adminhtml|frontend)\//', $file, $matches)) {
$area = $matches['area'];
}
return $area;
}
/**
* Check layout block dependency
*
* Return: array(
* 'modules' // dependent modules
* 'source' // source text
* )
*
* @param string $currentModule
* @param string|null $area
* @param string $block
* @return array
*/
protected function _checkDependencyLayoutBlock(string $currentModule, ?string $area, string $block): array
{
if (isset($this->_mapLayoutBlocks[$area][$block]) || $area === null) {
// CASE 1: No dependencies
$modules = [];
if ($area === null) {
foreach ($this->_mapLayoutBlocks as $blocks) {
if (array_key_exists($block, $blocks)) {
$modules += $blocks[$block];
}
}
} else {
$modules = $this->_mapLayoutBlocks[$area][$block];
}
if (isset($modules[$currentModule])) {
return ['modules' => []];
}
// CASE 2: Single dependency
if (1 == count($modules)) {
return ['modules' => $modules];
}
// CASE 3: Default module dependency
$defaultModule = $this->_getDefaultModuleName($area);
if (isset($modules[$defaultModule])) {
return ['modules' => [$defaultModule]];
}
}
// CASE 4: \Exception - Undefined block
return [];
}
/**
* Retrieve default module name (by area)
*
* @param string $area
* @return null
*/
protected function _getDefaultModuleName($area = 'default')
{
if (isset($this->_defaultModules[$area])) {
return $this->_defaultModules[$area];
}
return null;
}
/**
* Retrieve unique dependencies
*
* @param array $dependencies
* @return array
*/
protected function _getUniqueDependencies($dependencies = [])
{
$result = [];
foreach ($dependencies as $module => $value) {
$result[] = ['modules' => [$module], 'type' => $value['type'], 'source' => $value['source']];
}
return $result;
}
/**
* Merge dependencies
*
* @param array $known
* @param array $new
* @return array
*/
private function considerCaseDependencies($known, $new)
{
return array_merge($known, $new);
}
}
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Dependency\Reader;
use Magento\Setup\Module\Di\Code\Reader\FileClassScanner;
/**
* Search classes in file by path.
*/
class ClassScanner
{
/**
* @var string[]
*/
private $classNames = [];
/**
* Get class name by file name.
*
* @param string $filePath
*
* @return string
*/
public function getClassName(string $filePath): string
{
if (!isset($this->classNames[$filePath])) {
$this->classNames[$filePath] = $this->loadClassName($filePath);
}
return $this->classNames[$filePath];
}
/**
* Load class name from file.
*
* @param string $filePath
*
* @return string
*/
private function loadClassName(string $filePath): string
{
$scanner = new FileClassScanner($filePath);
return $scanner->getClassName();
}
}
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Dependency;
/**
* Class provides dependency rule for reports.xml config file.
*/
class ReportsConfigRule implements RuleInterface
{
/**
* Map of tables and modules
*
* @var array
*/
private $moduleTableMap;
/**
* @param array $tables
*/
public function __construct(array $tables)
{
$this->moduleTableMap = $tables;
}
/**
* @inheritdoc
*/
public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
{
if ('config' != $fileType || !preg_match('#.*/reports\.xml$#', $file)) {
return [];
}
$dependenciesInfo = [];
if (preg_match_all('#<source[^>]*name=[\'"]([^\'"]+)[\'"]#i', $contents, $matches)) {
$tables = array_pop($matches);
foreach ($tables as $table) {
if (!isset($this->moduleTableMap[$table])) {
continue;
}
if (strtolower($currentModule) !== strtolower($this->moduleTableMap[$table])) {
$dependenciesInfo[] = [
'modules' => [$this->moduleTableMap[$table]],
'type' => RuleInterface::TYPE_HARD,
'source' => $table,
];
}
}
}
return $dependenciesInfo;
}
}
@@ -0,0 +1,337 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Dependency\Route;
use Exception;
use Magento\Framework\App\Area;
use Magento\Framework\App\Utility\Files;
use Magento\Framework\Component\ComponentFile;
use Magento\TestFramework\Exception\NoSuchActionException;
/**
* Route mapper based on routes.xml declarations
*/
class RouteMapper
{
/**
* List of routers
*
* Format: array(
* '{Router_Id}' => '{Route_Id}' => ['{Module_Name}']
* )
*
* @var array
*/
private $routers = [];
/**
* List of routes.xml files by modules
*
* Format: array(
* '{Module_Name}' => ['{Filename}']
* )
*
* @var array
*/
private $routeConfigFiles = [];
/**
* List of controllers actions
*
* Format: array(
* '{Router_Id}' => '{Route_Id}' => '{Controller_Name}' => '{Action_Name}' => [{'Module_Name'}]
* )
*
* @var array
*/
private $actions = [];
/**
* List of reserved words
*
* @see \Magento\Framework\App\Router\ActionList
* @var array
*/
private $reservedWords = [
'abstract' => 1,
'and' => 1,
'array' => 1,
'as' => 1,
'break' => 1,
'callable' => 1,
'case' => 1,
'catch' => 1,
'class' => 1,
'clone' => 1,
'const' => 1,
'continue' => 1,
'declare' => 1,
'default' => 1,
'die' => 1,
'do' => 1,
'echo' => 1,
'else' => 1,
'elseif' => 1,
'empty' => 1,
'enddeclare' => 1,
'endfor' => 1,
'endforeach' => 1,
'endif' => 1,
'endswitch' => 1,
'endwhile' => 1,
'eval' => 1,
'exit' => 1,
'extends' => 1,
'final' => 1,
'finally' => 1,
'fn' => 1,
'for' => 1,
'foreach' => 1,
'function' => 1,
'global' => 1,
'goto' => 1,
'if' => 1,
'implements' => 1,
'include' => 1,
'instanceof' => 1,
'insteadof' => 1,
'interface' => 1,
'isset' => 1,
'list' => 1,
'match' => 1,
'namespace' => 1,
'new' => 1,
'or' => 1,
'print' => 1,
'private' => 1,
'protected' => 1,
'public' => 1,
'require' => 1,
'return' => 1,
'static' => 1,
'switch' => 1,
'throw' => 1,
'trait' => 1,
'try' => 1,
'unset' => 1,
'use' => 1,
'var' => 1,
'void' => 1,
'while' => 1,
'xor' => 1,
'yield' => 1
];
/**
* Provide routing declaration by router_id
*
* @param string $routerId
* @return array
* @throws Exception
*/
public function getRoutes(string $routerId = ''): array
{
$routes = [];
if (!$routerId) {
foreach ($this->getRoutersMap() as $routesByRouterType) {
$routes = array_merge_recursive($routes, $routesByRouterType);
}
array_walk(
$routes,
function (&$modules) {
$modules = array_unique($modules);
}
);
} else {
$routes = $this->getRoutersMap()[$routerId] ?? [];
}
return $routes;
}
/**
* Provide dependencies for a specific URL path
*
* @param string $routeId
* @param string $controllerName
* @param string $actionName
* @return string[]
* @throws NoSuchActionException
* @throws Exception
*/
public function getDependencyByRoutePath(
string $routeId,
string $controllerName,
string $actionName
): array {
$routeId = strtolower($routeId);
$controllerName = strtolower($controllerName);
$actionName = strtolower($actionName);
if (isset($this->reservedWords[$actionName])) {
$actionName .= 'action';
}
$dependencies = [];
foreach ($this->getRouterTypes() as $routerId) {
if (isset($this->getActionsMap()[$routerId][$routeId][$controllerName][$actionName])) {
$dependencies[] = $this->getActionsMap()[$routerId][$routeId][$controllerName][$actionName];
}
}
$dependencies = array_merge([], ...$dependencies);
if (empty($dependencies)) {
throw new NoSuchActionException(implode('/', [$routeId, $controllerName, $actionName]));
} else {
$dependencies = array_unique($dependencies);
}
return $dependencies;
}
/**
* Provide a list of router type
*
* @return array
* @throws Exception
*/
private function getRouterTypes()
{
return array_keys($this->getActionsMap());
}
/**
* Provide routing declaration
*
* @return array
* @throws Exception
*/
private function getRoutersMap()
{
if (empty($this->routers)) {
foreach ($this->getListRoutesXml() as $module => $configFiles) {
foreach ($configFiles as $configFile) {
$this->processConfigFile($module, $configFile);
}
}
}
return $this->routers;
}
/**
* Update routers map for the module basing on the routing config file
*
* @param string $module
* @param string $configFile
*
* @return void
*/
private function processConfigFile(string $module, string $configFile)
{
// Read module's routes.xml file
$config = simplexml_load_file($configFile);
$routers = $config->xpath("/config/router");
foreach ($routers as $router) {
$routerId = (string)$router['id'];
foreach ($router->xpath('route') as $route) {
$routeId = (string)$route['id'];
if (!isset($this->routers[$routerId][$routeId])) {
$this->routers[$routerId][$routeId] = [];
}
if (!in_array($module, $this->routers[$routerId][$routeId])) {
$this->routers[$routerId][$routeId][] = $module;
}
}
}
}
/**
* Prepare the list of routes.xml files (by modules)
*
* @throws Exception
*/
private function getListRoutesXml()
{
if (empty($this->routeConfigFiles)) {
$files = Files::init()->getConfigFiles('*/routes.xml', [], false, true);
/** @var ComponentFile $componentFile */
foreach ($files as $componentFile) {
$module = str_replace('_', '\\', $componentFile->getComponentName());
$this->routeConfigFiles[$module][] = $componentFile->getFullPath();
}
}
return $this->routeConfigFiles;
}
/**
* Provide a list of available actions
*
* @return array
* @throws Exception
*/
private function getActionsMap(): array
{
if (empty($this->actions)) {
$files = Files::init()->getPhpFiles(Files::INCLUDE_APP_CODE);
$actionsMap = [];
foreach ($this->getRoutersMap() as $routerId => $routes) {
$actionsMapPerArea = [];
foreach ($routes as $routeId => $dependencies) {
$actionsMapPerArea[$routeId] = [];
foreach ($dependencies as $module) {
$moduleActions = $this->getModuleActionsMapping($module, $routerId, $files);
$actionsMapPerArea[$routeId] =
array_merge_recursive($actionsMapPerArea[$routeId], $moduleActions);
}
}
$actionsMap[$routerId] = $actionsMapPerArea;
}
$this->actions = $actionsMap;
}
return $this->actions;
}
/**
* Provide a list of available module actions by router_id
*
* @param string $module
* @param string $routerId
* @param array $files
* @return array
*/
private function getModuleActionsMapping(string $module, string $routerId, array $files): array
{
$subdirectoryPattern = str_replace('\\', DIRECTORY_SEPARATOR, $module);
$subdirectoryPattern .= DIRECTORY_SEPARATOR . 'Controller/';
if (array_search($routerId, [Area::AREA_ADMINHTML, Area::AREA_ADMIN], true) !== false) {
$subdirectoryPattern .= ucfirst(Area::AREA_ADMINHTML) . DIRECTORY_SEPARATOR;
} else {
$subdirectoryPattern .= '(?!' . ucfirst(Area::AREA_ADMINHTML) . ')';
}
$actions = preg_grep('~' . $subdirectoryPattern . '~', $files);
$actionsPattern = '#' . $subdirectoryPattern . '(?<controller>\S+)/'
. '(?<action_name>\w+)\.php$#';
$moduleActionsMapping = [];
foreach ($actions as $action) {
if (preg_match($actionsPattern, $action, $matches)) {
$controllerName = strtolower(str_replace('/', '_', $matches['controller']));
$actionName = strtolower($matches['action_name']);
if (!isset($moduleActionsMapping[$controllerName][$actionName])) {
$moduleActionsMapping[$controllerName][$actionName] = [];
}
$moduleActionsMapping[$controllerName][$actionName][] = $module;
}
}
return $moduleActionsMapping;
}
}
@@ -0,0 +1,29 @@
<?php
/**
* Rule for searching dependencies in layout files
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Dependency;
interface RuleInterface
{
/**
* Types of dependencies between modules
*/
const TYPE_SOFT = 'soft';
const TYPE_HARD = 'hard';
/**
* Gets alien dependencies information for current module by analyzing file's contents
*
* @param string $currentModule
* @param string $fileType
* @param string $file
* @param string $contents
* @return array
*/
public function getDependencyInfo($currentModule, $fileType, $file, &$contents);
}
@@ -0,0 +1,107 @@
<?php
/**
* Rule for searching php file dependency
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Dependency\VirtualType;
use DOMDocument;
use Magento\Framework\App\Utility\Files;
class VirtualTypeMapper
{
/**
* @var array
*/
private $map = [];
/**
* @var string
*/
private static $mainScope = 'global';
/**
* VirtualTypeMapper constructor.
* @param array $map
*/
public function __construct(array $map = [])
{
$this->map = $map;
}
/**
* @param string $name
* @param string $scope
* @return string|null
*/
public function getType($name, $scope = null)
{
if (empty($this->map)) {
$this->loadMap();
}
$scopes = [];
if ($scope !== null && $scope !== self::$mainScope) {
array_unshift($scopes, $scope);
}
$scopes[] = self::$mainScope;
foreach ($scopes as $scp) {
if (isset($this->map[$scp][$name])) {
return $this->map[$scp][$name];
}
}
return $name;
}
/**
* @param string $file
* @return string
*/
public function getScopeFromFile($file)
{
$basename = basename(pathinfo($file, PATHINFO_DIRNAME));
return $basename === 'etc' ? 'global' : $basename;
}
/**
* @return array
* @throws \Exception
*/
private function loadDiConfigs()
{
return Files::init()->getDiConfigs();
}
/**
* @param array $diFiles
* @return array
* @throws \Exception
*/
public function loadMap(array $diFiles = [])
{
if (empty($diFiles)) {
$diFiles = $this->loadDiConfigs();
}
foreach ($diFiles as $file) {
$scope = $this->getScopeFromFile($file);
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($file));
$nodes = $doc->getElementsByTagName('virtualType');
/** @var \DOMElement $node */
foreach ($nodes as $node) {
$name = $node->getAttribute('name');
$type = $node->getAttribute('type');
if (empty($type)) {
continue;
}
$this->map[$scope][$name] = $type;
}
}
return $this->map;
}
}
@@ -0,0 +1,16 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Exception;
/**
* An exception is thrown if such a controller action does not exist
*/
class NoSuchActionException extends \Exception
{
}
@@ -0,0 +1,160 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Abstract shell command for the static code inspection
*/
namespace Magento\TestFramework\Inspection;
/**
* Abstract class for commands
*/
abstract class AbstractCommand
{
/**
* @var string
*/
protected $_reportFile;
/**
* @var int
*/
protected $_lastExitCode;
/**
* @var string
*/
protected $_lastOutput;
/**
* @var string
*/
protected $_lastRunMessage;
/**
* Constructor
*
* @param string $reportFile Destination file to write inspection report to
*/
public function __construct($reportFile)
{
$this->_reportFile = $reportFile;
}
/**
* Build and execute the shell command
*
* @param array $whiteList Files/directories to be inspected
* @param array $blackList Files/directories to be excluded from the inspection
* @return bool
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
*/
public function run(array $whiteList, array $blackList = [])
{
if (file_exists($this->_reportFile)) {
unlink($this->_reportFile);
}
$shellCmd = $this->_buildShellCmd($whiteList, $blackList);
$result = $this->_execShellCmd($shellCmd);
$this->_generateLastRunMessage();
return $result !== false;
}
/**
* Whether the command can be ran on the current environment
*
* @return bool
*/
public function canRun()
{
return $this->_execShellCmd($this->_buildVersionShellCmd()) !== false;
}
/**
* Retrieve the shell command version
*
* @return string|null
*/
public function getVersion()
{
$versionOutput = $this->_execShellCmd($this->_buildVersionShellCmd());
if (!$versionOutput) {
return null;
}
return preg_match('/[^\d]*([^\s]+)/', $versionOutput, $matches) ? $matches[1] : $versionOutput;
}
/**
* Get path to the report file
*
* @return string
*/
public function getReportFile()
{
return $this->_reportFile;
}
/**
* Build the shell command that outputs the version
*
* @return string
*/
abstract protected function _buildVersionShellCmd();
/**
* Build the valid shell command
*
* @param array $whiteList
* @param array $blackList
* @return string
*/
abstract protected function _buildShellCmd($whiteList, $blackList);
/**
* Execute a shell command on the current environment and return its output or FALSE on failure
*
* @param string $shellCmd
* @return string|false
*/
protected function _execShellCmd($shellCmd)
{
$output = [];
//phpcs:disable
exec($shellCmd . ' 2>&1', $output, $this->_lastExitCode);
//phpcs:enable
$this->_lastOutput = implode(PHP_EOL, $output);
return $this->_lastExitCode === 0 ? $this->_lastOutput : false;
}
/**
* Generate message about last execution result, prepared for output to a user
*
* @return \Magento\TestFramework\Inspection\AbstractCommand
*/
protected function _generateLastRunMessage()
{
if ($this->_lastExitCode === null) {
$this->_lastRunMessage = "Nothing was executed.";
} elseif (!$this->_lastExitCode) {
$this->_lastRunMessage = 'Success reported.';
} elseif (file_exists($this->_reportFile)) {
$this->_lastRunMessage = "See detailed report in '{$this->_reportFile}'.";
} else {
$this->_lastRunMessage = 'Command-line tool reports: ' . $this->_lastOutput;
}
return $this;
}
/**
* Return message from the last run of the command
*
* @return string
*/
public function getLastRunMessage()
{
return $this->_lastRunMessage;
}
}
@@ -0,0 +1,14 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* \Exceptional situation during the static inspection
*/
namespace Magento\TestFramework\Inspection;
class Exception extends \Exception
{
}
@@ -0,0 +1,380 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Finder for a list of preconfigured words
*/
namespace Magento\TestFramework\Inspection;
class WordsFinder
{
/**
* List of file extensions, that indicate a binary file
*
* @var array
*/
protected $_binaryExtensions = [
'jpg', 'jpeg', 'png', 'gif', 'swf', 'mp3', 'avi', 'mov', 'flv', 'jar', 'zip',
'eot', 'ttf', 'woff', 'woff2', 'ico', 'svg',
];
/**
* Copyright string which must be present in every non-binary file
*
* @var string
*/
protected $copyrightString = 'Copyright © Magento, Inc. All rights reserved.';
/**
* Copying string which must be present in every non-binary file right after copyright string
*
* @var string
*/
protected $copyingString = 'See COPYING.txt for license details.';
/**
* List of extensions for which copyright check must be skipped
*
* @var array
*/
protected $copyrightSkipExtensions = ['csv', 'json', 'lock', 'md', 'txt'];
/**
* List of paths where copyright check must be skipped
*
* @var array
*/
protected $copyrightSkipList = [
'lib/web/legacy-build.min.js'
];
/**
* Whether copyright presence should be checked or not
*
* @var bool
*/
protected $isCopyrightChecked;
/**
* Words to search for
*
* @var array
*/
protected $_words = [];
/**
* Map of whitelisted paths to whitelisted words
*
* @var array
*/
protected $_whitelist = [];
/**
* Path to base dir, used to calculate relative paths
*
* @var string
*/
protected $_baseDir;
/**
* Component Registrar
*
* @var \Magento\Framework\Component\ComponentRegistrar
*/
protected $componentRegistrar;
/**
* Map of phrase to exclude from the file content
*
* @var array
*/
private $exclude = [];
/**
* @param string|array $configFiles
* @param string $baseDir
* @param \Magento\Framework\Component\ComponentRegistrar $componentRegistrar
* @param bool $isCopyrightChecked
* @throws \Magento\TestFramework\Inspection\Exception
*/
public function __construct($configFiles, $baseDir, $componentRegistrar, $isCopyrightChecked = false)
{
if (!is_dir($baseDir)) {
throw new \Magento\TestFramework\Inspection\Exception("Base directory {$baseDir} does not exist");
}
$this->_baseDir = str_replace('\\', '/', realpath($baseDir));
$this->componentRegistrar = $componentRegistrar;
// Load config files
if (!is_array($configFiles)) {
$configFiles = [$configFiles];
}
foreach ($configFiles as $configFile) {
$this->_loadConfig($configFile);
}
// Add config files to whitelist, as they surely contain banned words
foreach ($configFiles as $configFile) {
$configFile = str_replace('\\', '/', realpath($configFile));
$this->_whitelist[$configFile] = [];
}
$this->_normalizeWhitelistPaths();
// Final verifications
if (!$this->_words) {
throw new \Magento\TestFramework\Inspection\Exception('No words to check');
}
$this->isCopyrightChecked = $isCopyrightChecked;
}
/**
* Load configuration from file, adding words and whitelisted entries to main config
*
* @param string $configFile
* @throws \Magento\TestFramework\Inspection\Exception
*/
protected function _loadConfig($configFile)
{
if (!file_exists($configFile)) {
throw new \Magento\TestFramework\Inspection\Exception("Configuration file {$configFile} does not exist");
}
try {
$xml = new \SimpleXMLElement(file_get_contents($configFile));
} catch (\Exception $e) {
throw new \Magento\TestFramework\Inspection\Exception($e->getMessage(), $e->getCode(), $e);
}
$this->_extractWords($xml)->_extractWhitelist($xml);
}
/**
* Extract words from configuration xml
*
* @param \SimpleXMLElement $configXml
* @return \Magento\TestFramework\Inspection\WordsFinder
* @throws \Magento\TestFramework\Inspection\Exception
*/
protected function _extractWords(\SimpleXMLElement $configXml)
{
$words = [];
$nodes = $configXml->xpath('//config/words/word');
foreach ($nodes as $node) {
$words[] = (string)$node;
}
$words = array_filter($words);
$words = array_merge($this->_words, $words);
$this->_words = array_unique($words);
return $this;
}
/**
* Extract whitelisted entries and words from configuration xml
*
* @param \SimpleXMLElement $configXml
* @return \Magento\TestFramework\Inspection\WordsFinder
* @throws \Magento\TestFramework\Inspection\Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _extractWhitelist(\SimpleXMLElement $configXml)
{
// Load whitelist entries
$whitelist = [];
$exclude = [];
$nodes = $configXml->xpath('//config/whitelist/item');
foreach ($nodes as $node) {
$path = $node->xpath('path');
if (!$path) {
throw new \Magento\TestFramework\Inspection\Exception(
'A "path" must be defined for the whitelisted item'
);
}
$component = $node->xpath('component');
if ($component) {
$componentType = $component[0]->xpath('@type')[0];
$componentName = $component[0]->xpath('@name')[0];
$path = $this->componentRegistrar->getPath((string)$componentType, (string)$componentName)
. '/' . (string)$path[0];
} else {
$path = $this->_baseDir . '/' . (string)$path[0];
}
// Words
$words = [];
$wordNodes = $node->xpath('word');
if ($wordNodes) {
foreach ($wordNodes as $wordNode) {
$words[] = (string)$wordNode;
}
}
$whitelist[$path] = $words;
$excludeNodes = $node->xpath('exclude');
$excludes = [];
if ($excludeNodes) {
foreach ($excludeNodes as $extractNode) {
$excludes[] = (string)$extractNode;
}
}
if (isset($exclude[$path])) {
$exclude[$path] = array_merge($excludes, $exclude[$path]);
} else {
$exclude[$path] = $excludes;
}
}
// Merge with already present whitelist
foreach ($whitelist as $newPath => $newWords) {
if (isset($this->_whitelist[$newPath])) {
$newWords = array_merge($this->_whitelist[$newPath], $newWords);
}
$this->_whitelist[$newPath] = array_unique($newWords);
}
foreach ($exclude as $newPath => $newWords) {
if (isset($this->exclude[$newPath])) {
$newWords = array_merge($this->exclude[$newPath], $newWords);
}
$this->exclude[$newPath] = array_unique($newWords);
}
return $this;
}
/**
* Normalize whitelist paths, so that they containt only native directory separators
*/
protected function _normalizeWhitelistPaths()
{
$whitelist = $this->_whitelist;
$this->_whitelist = [];
foreach ($whitelist as $whitelistFile => $whitelistWords) {
$whitelistFile = str_replace('\\', '/', $whitelistFile);
$this->_whitelist[$whitelistFile] = $whitelistWords;
}
}
/**
* Checks the file content and name against the list of words. Do not check content of binary files.
* Exclude whitelisted entries.
*
* @param string $file
* @return array Words, found
*/
public function findWords($file)
{
$foundWords = $this->_findWords($file);
if (!$foundWords) {
return [];
}
return self::_removeWhitelistedWords($file, $foundWords);
}
/**
* Checks the file content and name against the list of words. Do not check content of binary files.
*
* @param string $file
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _findWords($file)
{
$checkContents = !$this->_isBinaryFile($file);
$path = $this->getSearchablePath($file);
$contents = $checkContents ? file_get_contents($file) : '';
if (isset($this->exclude[$file]) && !empty($this->exclude[$file])) {
foreach ($this->exclude[$file] as $stringToEliminate) {
$contents = str_replace($stringToEliminate, "", $contents);
}
}
$foundWords = [];
foreach ($this->_words as $word) {
if (stripos($path, $word) !== false || stripos($contents, $word) !== false) {
$foundWords[] = $word;
}
}
if ($contents && $this->isCopyrightChecked && !$this->isCopyrightCheckSkipped($file)
&& (($copyrightStringPosition = mb_strpos($contents, $this->copyrightString)) === false
|| ($copyingStringPosition = strpos($contents, $this->copyingString)) === false
|| $copyingStringPosition - $copyrightStringPosition - mb_strlen($this->copyrightString) > 10)
) {
$foundWords[] = 'Copyright string is missing';
}
return $foundWords;
}
/**
* @param string $path
* @return bool
*/
protected function isCopyrightCheckSkipped($path)
{
if (in_array(pathinfo($path, PATHINFO_EXTENSION), $this->copyrightSkipExtensions)) {
return true;
}
foreach ($this->copyrightSkipList as $dir) {
if (strpos($path, $dir) !== false) {
return true;
}
}
return false;
}
/**
* Check, whether file is a binary one
*
* @param string $file
* @return bool
*/
protected function _isBinaryFile($file)
{
return in_array(pathinfo($file, PATHINFO_EXTENSION), $this->_binaryExtensions);
}
/**
* Removes whitelisted words from array of found words
*
* @param string $path
* @param array $foundWords
* @return array
*/
protected function _removeWhitelistedWords($path, $foundWords)
{
$path = str_replace('\\', '/', $path);
foreach ($this->_whitelist as $whitelistPath => $whitelistWords) {
if (strncmp($whitelistPath, $path, strlen($whitelistPath)) != 0) {
continue;
}
if (!$whitelistWords) {
// All words are permitted there
return [];
}
$foundWords = array_diff($foundWords, $whitelistWords);
}
return $foundWords;
}
/**
* Return the path for words search
*
* @param string $file
* @return string
*/
protected function getSearchablePath($file)
{
if (strpos($file, $this->_baseDir) === false) {
return $file;
}
return substr($file, strlen($this->_baseDir) + 1);
}
}
@@ -0,0 +1,213 @@
<?php
/**
* An abstract test class for XML/XSD validation
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity;
abstract class AbstractConfig extends \PHPUnit\Framework\TestCase
{
public function testXmlFiles()
{
if (null === $this->_getXmlName()) {
$this->markTestSkipped('No XML validation of files requested');
}
$invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
$invoker(
/**
* @param string $configFile
*/
function ($configFile) {
$this->_validateFileExpectSuccess($configFile, $this->_getXsd(), $this->_getFileXsd());
},
\Magento\Framework\App\Utility\Files::init()->getConfigFiles($this->_getXmlName())
);
}
public function testSchemaUsingValidXml()
{
$xmlFile = $this->_getKnownValidXml();
$schema = $this->_getXsd();
$this->_validateFileExpectSuccess($xmlFile, $schema);
}
public function testSchemaUsingInvalidXml($expectedErrors = null)
{
if (!function_exists('libxml_set_external_entity_loader')) {
$this->markTestSkipped('Skipped due to MAGETWO-45033');
}
$xmlFile = $this->_getKnownInvalidXml();
$schema = $this->_getXsd();
$this->_validateFileExpectFailure($xmlFile, $schema, $expectedErrors);
}
public function testFileSchemaUsingPartialXml()
{
if (!function_exists('libxml_set_external_entity_loader')) {
$this->markTestSkipped('Skipped due to MAGETWO-45033');
}
$xmlFile = $this->_getKnownValidPartialXml();
if ($xmlFile === null) {
$this->markTestSkipped('No Partial File');
return;
}
$schema = $this->_getFileXsd();
$this->_validateFileExpectSuccess($xmlFile, $schema);
}
public function testFileSchemaUsingInvalidXml($expectedErrors = null)
{
if (!function_exists('libxml_set_external_entity_loader')) {
$this->markTestSkipped('Skipped due to MAGETWO-45033');
}
$xmlFile = $this->_getKnownInvalidPartialXml();
if ($xmlFile === null) {
$this->markTestSkipped('No Partial File');
return;
}
$schema = $this->_getFileXsd();
$this->_validateFileExpectFailure($xmlFile, $schema, $expectedErrors);
}
public function testSchemaUsingPartialXml($expectedErrors = null)
{
if (!function_exists('libxml_set_external_entity_loader')) {
$this->markTestSkipped('Skipped due to MAGETWO-45033');
}
$xmlFile = $this->_getKnownValidPartialXml();
if ($xmlFile === null) {
$this->markTestSkipped('No Partial File');
return;
}
$schema = $this->_getXsd();
$this->_validateFileExpectFailure($xmlFile, $schema, $expectedErrors);
}
/**
* Run schema validation against a known bad xml file with a provided schema.
*
* This helper expects the validation to fail and will fail a test if no errors are found.
*
* @param $xmlFile string a known bad xml file.
* @param $schemaFile string schema that should find errors in the known bad xml file.
* @param $fileSchemaFile string schema that should find errors in the known bad xml file
*/
protected function _validateFileExpectSuccess($xmlFile, $schemaFile, $fileSchemaFile = null)
{
$dom = new \DOMDocument();
$dom->loadXML(file_get_contents($xmlFile));
$errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schemaFile);
if ($errors) {
if ($fileSchemaFile !== null) {
$moreErrors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $fileSchemaFile);
if (empty($moreErrors)) {
return;
} else {
$errors = array_merge($errors, $moreErrors);
}
}
$this->fail(
'There is a problem with the schema. A known good XML file failed validation: ' . PHP_EOL . implode(
PHP_EOL . PHP_EOL,
$errors
)
);
}
}
/**
* Run schema validation against an xml file with a provided schema.
*
* This helper expects the validation to pass and will fail a test if any errors are found.
*
* @param $xmlFile string a known good xml file.
* @param $schemaFile string schema that should find no errors in the known good xml file.
* @param $expectedErrors null|array that may contain a list of expected errors. Each element can be a substring
* of an error, but all errors must be listed.
*/
protected function _validateFileExpectFailure($xmlFile, $schemaFile, $expectedErrors = null)
{
$dom = new \DOMDocument();
$dom->loadXML(file_get_contents($xmlFile));
$actualErrors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schemaFile);
if (isset($expectedErrors)) {
$this->assertNotEmpty(
$actualErrors,
'No schema validation errors found, expected errors: ' . PHP_EOL . implode(PHP_EOL, $expectedErrors)
);
foreach ($expectedErrors as $expectedError) {
$found = false;
foreach ($actualErrors as $errorKey => $actualError) {
if (!(strpos($actualError, $expectedError) === false)) {
// found expected string
$found = true;
// remove found error from list of actual errors
unset($actualErrors[$errorKey]);
break;
}
}
$this->assertTrue(
$found,
'Failed asserting that ' . $expectedError . " is in: \n" . implode(PHP_EOL, $actualErrors)
);
}
// list of actual errors should now be empty
$this->assertEmpty($actualErrors, "There were unexpected errors: \n" . implode(PHP_EOL, $actualErrors));
} elseif (!$actualErrors) {
$this->fail('There is a problem with the schema. A known bad XML file passed validation');
}
}
/**
* Returns the name of the XSD file to be used to validate the XML
*
* @return string
*/
abstract protected function _getXsd();
/**
* The location of a single valid complete xml file
*
* @return string
*/
abstract protected function _getKnownValidXml();
/**
* The location of a single known invalid complete xml file
*
* @return string
*/
abstract protected function _getKnownInvalidXml();
/**
* The location of a single known valid partial xml file
*
* @return string
*/
abstract protected function _getKnownValidPartialXml();
/**
* Returns the name of the XSD file to be used to validate partial XML
*
* @return string
*/
abstract protected function _getFileXsd();
/**
* The location of a single known invalid partial xml file
*
* @return string
*/
abstract protected function _getKnownInvalidPartialXml();
/**
* Returns the name of the xml files to validate
*
* @return string
*/
abstract protected function _getXmlName();
}
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library;
use Laminas\Code\Reflection\ClassReflection;
use Magento\Framework\GetParameterClassTrait;
use ReflectionException;
/**
* Provide dependencies for the file
*/
class Injectable
{
use GetParameterClassTrait;
/**
* @var string[]
*/
protected $dependencies = [];
/**
* Get dependencies
*
* @param ClassReflection $class
*
* @return string[]
* @throws ReflectionException
*/
public function getDependencies(ClassReflection $class): array
{
foreach ($class->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() !== $class->getName()) {
continue;
}
foreach ($method->getParameters() as $parameter) {
try {
$dependency = $this->getParameterClass($parameter);
if ($dependency !== null) {
$this->dependencies[] = $dependency->getName();
}
} catch (ReflectionException $e) {
if (preg_match('#^Class ([A-Za-z0-9_\"\\\\]+) does not exist$#', $e->getMessage(), $result)) {
$this->dependencies[] = trim($result[1], '"');
} else {
throw $e;
}
}
}
}
return $this->dependencies;
}
}
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library\PhpParser;
/**
* Collect dependencies
*
*/
interface DependenciesCollectorInterface
{
/**
* Return list of dependencies
*
* @param Uses $uses
* @return string[]
*/
public function getDependencies(Uses $uses);
}
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library\PhpParser;
/**
* Class know how create any parser
*
*/
class ParserFactory
{
/**
* @var ParserInterface[]
*/
protected $parsers = [];
/**
* @var Uses
*/
protected $uses;
/**
* @var StaticCalls
*/
protected $staticCalls;
/**
* @var Throws
*/
protected $throws;
/**
* @var Tokens
*/
protected $tokens;
/**
* Return all parsers
*
* @param Tokens $tokens
* @return ParserInterface[]
*/
public function createParsers(Tokens $tokens)
{
if (empty($this->parsers)) {
$this->parsers = [
$this->uses = new Uses(),
$this->staticCalls = new StaticCalls($tokens),
$this->throws = new Throws($tokens),
];
}
return $this->parsers;
}
/**
* Get uses
*
* @return Uses
*/
public function getUses()
{
return $this->uses;
}
/**
* Get static calls
*
* @return StaticCalls
*/
public function getStaticCalls()
{
return $this->staticCalls;
}
/**
* Get throws
*
* @return Throws
*/
public function getThrows()
{
return $this->throws;
}
}
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library\PhpParser;
/**
* Parser for each token type
*
*/
interface ParserInterface
{
/**
* Parse specific token
*
* @param array|string $value
* @param int $key
*/
public function parse($value, $key);
}
@@ -0,0 +1,119 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library\PhpParser;
/**
* Parse static calls and collect dependencies for it
*
*/
class StaticCalls implements ParserInterface, DependenciesCollectorInterface
{
/**
* @var Tokens
*/
protected $tokens;
/**
* Save static calls token key
*
* @var array
*/
protected $staticCalls = [];
/**
* Collect dependencies
*
* @var array
*/
protected $dependencies = [];
/**
* @param Tokens $tokens
*/
public function __construct(Tokens $tokens)
{
$this->tokens = $tokens;
}
/**
* Check if it's foreign dependency
*
* @param array $token
* @return bool
*/
protected function isTokenClass($token)
{
return is_array(
$token
) && !(in_array(
$token[1],
['static', 'self', 'parent']
) || preg_match(
'#^\$#',
$token[1]
));
}
/**
* @inheritdoc
*/
public function parse($token, $key)
{
if (is_array(
$token
) && $token[0] == T_PAAMAYIM_NEKUDOTAYIM && $this->isTokenClass(
$this->tokens->getPreviousToken($key)
)
) {
$this->staticCalls[] = $key;
}
}
/**
* Return class name from token
*
* @param int $staticCall
* @return string
*/
protected function getClassByStaticCall($staticCall)
{
$step = 1;
$staticClassParts = [];
$token = $this->tokens->getTokenCodeByKey($staticCall - $step);
if ($token === T_NAME_FULLY_QUALIFIED || $token === T_NAME_QUALIFIED) {
return $this->tokens->getTokenValueByKey($staticCall - $step);
}
// PHP 7 compatibility
while ($this->tokens->getTokenCodeByKey(
$staticCall - $step
) == T_STRING || $this->tokens->getTokenCodeByKey(
$staticCall - $step
) == T_NS_SEPARATOR) {
$staticClassParts[] = $this->tokens->getTokenValueByKey($staticCall - $step);
$step++;
}
return implode(array_reverse($staticClassParts));
}
/**
* @inheritdoc
*/
public function getDependencies(Uses $uses)
{
foreach ($this->staticCalls as $staticCall) {
$class = $this->getClassByStaticCall($staticCall);
if ($uses->hasUses()) {
$class = $uses->getClassNameWithNamespace($class);
}
$this->dependencies[] = $class;
}
return $this->dependencies;
}
}
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library\PhpParser;
/**
* Parse throws and collect dependencies for it
*
*/
class Throws implements ParserInterface, DependenciesCollectorInterface
{
/**
* @var Tokens
*/
protected $tokens;
/**
* Collect dependencies
*
* @var array
*/
protected $dependencies = [];
/**
* Save throw token key
*
* @var array
*/
protected $throws = [];
/**
* @param Tokens $tokens
*/
public function __construct(Tokens $tokens)
{
$this->tokens = $tokens;
}
/**
* @inheritdoc
*/
public function parse($token, $key)
{
if (is_array($token) && $token[0] == T_THROW) {
$this->throws[] = $key;
}
}
/**
* @inheritdoc
*/
public function getDependencies(Uses $uses)
{
foreach ($this->throws as $throw) {
$class = '';
if ($this->tokens->getTokenCodeByKey($throw + 2) == T_NEW) {
$step = 4;
$token = $this->tokens->getTokenCodeByKey($throw + $step);
if ($token === T_NAME_FULLY_QUALIFIED || $token === T_NAME_QUALIFIED) {
$class = $this->tokens->getTokenValueByKey($throw + $step);
} else {
// PHP 7 compatibility
while ($this->tokens->getTokenCodeByKey($throw + $step) === T_STRING
|| $this->tokens->getTokenCodeByKey($throw + $step) === T_NS_SEPARATOR) {
$class .= trim($this->tokens->getTokenValueByKey($throw + $step));
$step++;
}
}
if ($uses->hasUses()) {
$class = $uses->getClassNameWithNamespace($class);
}
$this->dependencies[] = $class;
}
}
return $this->dependencies;
}
}
@@ -0,0 +1,120 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library\PhpParser;
/**
* Parse php code and found dependencies
*
*/
class Tokens
{
/**
* Collect all tokens
*
* @var array
*/
protected $tokens = [];
/**
* Collect dependencies
*
* @var array
*/
protected $dependencies = [];
/**
* Collect all parsers
*
* @var ParserInterface[]
*/
protected $parsers = [];
/**
* Parser factory for creating parsers
*
* @var ParserFactory
*/
protected $parserFactory;
/**
* @param string $content
* @param ParserFactory $parserFactory
*/
public function __construct($content, ParserFactory $parserFactory)
{
$this->tokens = token_get_all($content);
$this->parserFactory = $parserFactory;
}
/**
* Parse content
*/
public function parseContent()
{
foreach ($this->tokens as $k => $token) {
foreach ($this->getParsers() as $parser) {
$parser->parse($token, $k);
}
}
}
/**
* Get all parsers
*
* @return ParserInterface[]
*/
protected function getParsers()
{
return $this->parserFactory->createParsers($this);
}
/**
* Get parsed dependencies
*
* @return array
*/
public function getDependencies()
{
return array_merge(
$this->parserFactory->getStaticCalls()->getDependencies($this->parserFactory->getUses()),
$this->parserFactory->getThrows()->getDependencies($this->parserFactory->getUses())
);
}
/**
* Return previous token
*
* @param int $key
* @param int $step
* @return array
*/
public function getPreviousToken($key, $step = 1)
{
return $this->tokens[$key - $step];
}
/**
* Return token code by key
*
* @param $key
* @return null|int
*/
public function getTokenCodeByKey($key)
{
return is_array($this->tokens[$key]) ? $this->tokens[$key][0] : null;
}
/**
* Return token value by key
*
* @param $key
* @return string
*/
public function getTokenValueByKey($key)
{
return is_array($this->tokens[$key]) ? $this->tokens[$key][1] : $this->tokens[$key];
}
}
@@ -0,0 +1,135 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity\Library\PhpParser;
/**
* Parse uses block
*
*/
class Uses implements ParserInterface
{
/**
* Flag for parse use block
*
* @var bool
*/
protected $parseUse = false;
/**
* Collect all uses
*
* @var array
*/
protected $uses = [];
/**
* Check if uses present in content
*
* @return bool
*/
public function hasUses()
{
return !empty($this->uses);
}
/**
* Create empty uses in collection
*/
protected function createEmptyItem()
{
$this->uses[] = '';
}
/**
* Return class name with namespace
*
* @param string $class
* @return string
*/
public function getClassNameWithNamespace($class)
{
if (preg_match('#^\\\\#', $class)) {
return $class;
}
preg_match('#^([A-Za-z0-9_]+)(.*)$#', $class, $match);
foreach ($this->uses as $use) {
if (preg_match('#^([A-Za-z0-9_\\\\]+)\s+as\s+(.*)$#', $use, $useMatch) && $useMatch[2] == $match[1]) {
$class = $useMatch[1] . $match[2];
break;
}
$packages = explode('\\', $use);
end($packages);
$lastPackageKey = key($packages);
if ($packages[$lastPackageKey] == $match[1]) {
$class = $use . $match[2];
}
}
return $class;
}
/**
* Append part of uses into last item
*
* @param string $value
*/
protected function appendToLast($value)
{
end($this->uses);
$this->uses[key($this->uses)] = ltrim($this->uses[key($this->uses)] . $value);
}
/**
* Check flag parse
*
* @return bool
*/
protected function isParseInProgress()
{
return $this->parseUse;
}
/**
* Start parse
*/
protected function stopParse()
{
$this->parseUse = false;
}
/**
* Stop parse
*/
protected function startParse()
{
$this->parseUse = true;
}
/**
* @inheritdoc
*/
public function parse($token, $key)
{
if (is_array($token)) {
if ($this->isParseInProgress()) {
$this->appendToLast($token[1]);
}
if (T_USE == $token[0]) {
$this->startParse();
$this->createEmptyItem();
}
} else {
if ($this->isParseInProgress()) {
if ($token == ';') {
$this->stopParse();
}
if ($token == ',') {
$this->createEmptyItem();
}
}
}
}
}
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Integrity;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Interception\Code\InterfaceValidator;
use Magento\Framework\Phrase;
class PluginValidator
{
/** @var InterfaceValidator */
private $frameworkValidator;
/**
* @param InterfaceValidator $frameworkValidator
*/
public function __construct(InterfaceValidator $frameworkValidator)
{
$this->frameworkValidator = $frameworkValidator;
}
/**
* Validate plugin and intercepted class
*
* @param string $pluginClass
* @param string $interceptedType
* @throws ValidatorException
*/
public function validate($pluginClass, $interceptedType)
{
$this->frameworkValidator->validate($pluginClass, $interceptedType);
$this->validateClassNameMatchesCase($pluginClass);
$this->validateClassNameMatchesCase($interceptedType);
}
/**
* Verify that the class name has same capitalization as the class declaration
*
* @param string $className
* @throws ValidatorException
*/
private function validateClassNameMatchesCase($className)
{
$declarationName = (new \ReflectionClass($className))->getName();
if (ltrim($className, '\\') != ltrim($declarationName)) {
throw new ValidatorException(
new Phrase(
"Capitalization of class name '%1' does not match expected '%2'",
[$className, $declarationName]
)
);
}
}
}
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Utility;
/**
* Helper class to add list of added new files.
*/
class AddedFiles
{
/**
* Provide list of new files.
*
* @param string $changedFilesBaseDir
*
* @return string[]
*/
public static function getAddedFilesList(string $changedFilesBaseDir): array
{
return FilesSearch::getFilesFromListFile(
$changedFilesBaseDir,
'changed_files*.added.*',
function () {
// if no list files, probably, this is the dev environment
// phpcs:ignore Generic.PHP.NoSilencedErrors,Magento2.Security.InsecureFunction
@exec('git diff --cached --name-only --diff-filter=A', $addedFiles);
return $addedFiles;
}
);
}
}
@@ -0,0 +1,149 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Utility;
use ReflectionNamedType;
/**
* Find classes created by object manager that are not requested in constructor. All autogenerated classes should be
* requested in constructor, otherwise compiler will not be able to find and generate these classes
*/
class AutogeneratedClassNotInConstructorFinder
{
/**
* @var ClassNameExtractor
*/
private $classNameExtractor;
/**
* Constructor
*
* @param ClassNameExtractor $classNameExtractor
*/
public function __construct(
ClassNameExtractor $classNameExtractor
) {
$this->classNameExtractor = $classNameExtractor;
}
/**
* Find classes created by object manager that are not requested in constructor
*
* @param string $fileContent
* @return array
* @throws \ReflectionException
*/
public function find($fileContent)
{
$classNames = [];
preg_match_all(
'/(get|create)\(\s*([a-z0-9\\\\]+)::class\s*\)/im',
$fileContent,
$shortNameMatches
);
if (isset($shortNameMatches[2])) {
foreach ($shortNameMatches[2] as $shortName) {
if (substr($shortName, 0, 1) !== '\\') {
$class = $this->matchPartialNamespace($fileContent, $shortName);
} else {
$class = $shortName;
}
$class = ltrim($class, '\\');
if (\Magento\Framework\App\Utility\Classes::isVirtual($class)) {
continue;
}
$className = $this->classNameExtractor->getNameWithNamespace($fileContent);
if ($className) {
$arguments = $this->getConstructorArguments($className);
if (in_array($class, $arguments)) {
continue;
}
}
$classNames[] = $class;
}
}
return $classNames;
}
/**
* Get constructor arguments
*
* @param string $className
* @return string[]
* @throws \ReflectionException
*/
private function getConstructorArguments($className)
{
$arguments = [];
$reflectionClass = new \ReflectionClass($className);
$constructor = $reflectionClass->getConstructor();
if ($constructor) {
$classParameters = $constructor->getParameters();
foreach ($classParameters as $classParameter) {
if ($classParameter->getType()) {
/** @var ReflectionNamedType $parameterType */
$parameterType = $classParameter->getType();
$arguments[] = ltrim($parameterType->getName(), '\\');
}
}
}
return $arguments;
}
/**
* Match partial namespace
*
* @param string $fileContent
* @param string $shortName
* @return string
*/
private function matchPartialNamespace(string $fileContent, string $shortName): string
{
preg_match_all(
'/^use\s([a-z0-9\\\\]+' . str_replace('\\', '\\\\', $shortName) . ');$/im',
$fileContent,
$fullNameMatches
);
if (isset($fullNameMatches[1][0])) {
$class = $fullNameMatches[1][0];
} else {
preg_match_all(
'/^use\s([a-z0-9\\\\]+)\sas\s' . str_replace('\\', '\\\\', $shortName) . ';$/im',
$fileContent,
$fullNameAliasMatches
);
if (isset($fullNameAliasMatches[1][0])) {
$class = $fullNameAliasMatches[1][0];
} else {
$forwardSlashPos = strpos($shortName, '\\');
if (is_int($forwardSlashPos)) {
$partialNamespace = substr($shortName, 0, $forwardSlashPos);
} else {
$partialNamespace = $shortName;
}
preg_match_all(
'/^use\s([a-z0-9\\\\]+)' . $partialNamespace . ';$/im',
$fileContent,
$partialNamespaceMatches
);
if ($forwardSlashPos && isset($partialNamespaceMatches[1][0])) {
$class = $partialNamespaceMatches[1][0] . $shortName;
} else {
$class = $this->classNameExtractor->getNamespace($fileContent) . '\\' . $shortName;
}
}
}
return $class;
}
}
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility;
use Magento\Framework\App\Utility\Files;
/**
* A helper to gather various changed files
* if INCREMENTAL_BUILD env variable is set by CI build infrastructure, only files changed in the
* branch are gathered, otherwise all files
*/
class ChangedFiles
{
/**
* File path with changed files content.
*/
const CHANGED_FILES_CONTENT_FILE = '/dev/tests/static/testsuite/Magento/Test/_files/changed_%s_files_content.json';
/**
* Returns array of PHP-files, that use or declare Magento application classes and Magento libs
*
* @param string $changedFilesList
* @param int $fileTypes
* @return array
*/
public static function getPhpFiles($changedFilesList, $fileTypes = 0)
{
$fileUtilities = Files::init();
if (isset($_ENV['INCREMENTAL_BUILD'])) {
$phpFiles = [];
foreach (glob($changedFilesList, GLOB_NOSORT) as $listFile) {
$phpFiles = array_merge($phpFiles, file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
}
array_walk(
$phpFiles,
function (&$file) {
$file = BP . '/' . $file;
}
);
if (!empty($phpFiles)) {
$phpFiles = Files::composeDataSets($phpFiles);
$phpFiles = array_intersect_key($phpFiles, $fileUtilities->getPhpFiles($fileTypes));
}
} else {
$phpFiles = $fileUtilities->getPhpFiles($fileTypes);
}
return $phpFiles;
}
/**
* Get changed content.
*
* @param string $fileName
* @return string
*/
public static function getChangedContent($fileName)
{
$data = [];
$extension = self::getFileExtension($fileName);
$fileName = ltrim(str_replace(BP, '', $fileName), DIRECTORY_SEPARATOR);
$changedFilesContentFile = BP . sprintf(self::CHANGED_FILES_CONTENT_FILE, $extension);
if (file_exists($changedFilesContentFile)) {
$changedContent = file_get_contents($changedFilesContentFile);
$data = json_decode($changedContent, true);
}
return isset($data[$fileName]) ? $data[$fileName] : '';
}
/**
* Get file extension.
*
* @param string $fileName
* @return string
*/
public static function getFileExtension($fileName)
{
$fileInfo = pathinfo($fileName);
return isset($fileInfo['extension']) ? $fileInfo['extension'] : 'unknown';
}
}
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Utility;
/**
* Search for children classes in list of files.
*/
class ChildrenClassesSearch
{
/**
* @var ClassNameExtractor
*/
private $classNameExtractor;
/**
* ChildrenClassesSearch constructor.
*/
public function __construct()
{
$this->classNameExtractor = new ClassNameExtractor();
}
/**
* Get list of classes name which are subclasses of mentioned class.
*
* @param array $fileList
* @param string $parent
* @param bool $asDataSet
*
* @return array
* @throws \ReflectionException
*/
public function getClassesWhichAreChildrenOf(array $fileList, string $parent, bool $asDataSet = true): array
{
$found = [];
foreach ($fileList as $file) {
$name = $asDataSet ? $file[0] : $file;
$class = $this->classNameExtractor->getNameWithNamespace(file_get_contents($name));
if ($class) {
$classReflection = new \ReflectionClass($class);
if ($classReflection->isSubclassOf($parent)) {
$found[] = $class;
}
}
}
return $found;
}
}
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility;
class ClassNameExtractor
{
/**
* Get class name with namespace
*
* @param string $fileContent
* @return bool|string
*/
public function getNameWithNamespace($fileContent)
{
$namespace = $this->getNamespace($fileContent);
$name = $this->getName($fileContent);
if ($name) {
return $namespace ? $namespace . '\\' . $name : $name;
}
return false;
}
/**
* Get class name
*
* @param string $fileContent
* @return string|bool
*/
public function getName($fileContent)
{
$namespace = $this->getNamespace($fileContent);
if (isset($namespace)) {
preg_match_all(
'/^\s*(class|abstract\sclass|interface)\s+([a-z0-9]+)(\s+(extends|implements)|\s*$)/im',
$fileContent,
$classMatches
);
if (isset($classMatches[2][0])) {
return $classMatches[2][0];
}
}
return false;
}
/**
* Get class namespace
*
* @param string $fileContent
* @return string|bool
*/
public function getNamespace($fileContent)
{
preg_match_all(
'/namespace\s([a-z0-9\\\\]+);/im',
$fileContent,
$namespaceMatches
);
if (isset($namespaceMatches[1][0])) {
return $namespaceMatches[1][0];
}
return false;
}
}
@@ -0,0 +1,150 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility;
/**
* Searches for usage of classes, namespaces, functions, etc in PHP files
*/
class CodeCheck
{
/**
* Check if the class is used in the content
*
* @param string $className
* @param string $content
* @return bool
*/
public static function isClassUsed($className, $content)
{
/* avoid matching namespace instead of class */
$content = preg_replace('/namespace[^;]+;/', '', $content);
return self::_isRegexpMatched('/[^a-z\d_\$]' . preg_quote($className, '/') . '[^a-z\d_\\\\]/iS', $content);
}
/**
* Check if the namespace is used in the content
*
* @param string $namespace
* @param string $content
* @return bool
*/
public static function isNamespaceUsed($namespace, $content)
{
return self::_isRegexpMatched('/namespace\s+' . preg_quote($namespace, '/') . ';/S', $content)
|| self::_isRegexpMatched('/[^a-zA-Z\d_]' . preg_quote($namespace . '\\', '/') . '/S', $content);
}
/**
* Check if the specified function is called in the content.
* Note that declarations are not considered.
*
* If class context is not specified, invocation of all functions or methods (of any class)
* will be matched across the board
*
* If some class is specified, only its methods will be matched as follows:
* - usage of class::method
* - usage of $this, self and static within the class and its descendants
*
* @param string $method
* @param string $content
* @param string $class
* @return bool
*/
public static function isFunctionCalled($method, $content, $class = null)
{
$quotedMethod = preg_quote($method, '/');
if (!$class) {
return self::_isRegexpMatched(
'/(?<![a-z\d_:]|->|function\s)' . $quotedMethod . '\s*\(/iS',
$content
);
}
// without opening parentheses to match static callbacks notation
if (self::_isRegexpMatched(
'/' . preg_quote($class, '/') . '::\s*' . $quotedMethod . '[^a-z\d_]/iS',
$content
)
) {
return true;
}
if (self::isClassOrInterface($content, $class) || self::isDirectDescendant($content, $class)) {
return self::_isRegexpMatched('/this->' . $quotedMethod . '\s*\(/iS', $content)
|| self::_isRegexpMatched(
'/(self|static|parent)::\s*' . $quotedMethod . '\s*\(/iS',
$content
);
}
}
/**
* Check if methods or functions are used in the content
*
* If class context is specified, only the class and its descendants will be checked.
*
* @param string $property
* @param string $content
* @param string $class
* @return bool
*/
public static function isPropertyUsed($property, $content, $class = null)
{
if ($class) {
if (!self::isClassOrInterface($content, $class) && !self::isDirectDescendant($content, $class)) {
return false;
}
}
return self::_isRegexpMatched(
'/[^a-z\d_]' . preg_quote($property, '/') . '[^a-z\d_]/iS',
$content
);
}
/**
* Analyze content to determine whether it is a declaration of the specified class/interface
*
* @param string $content
* @param string $name
* @return bool
*/
public static function isClassOrInterface($content, $name)
{
return self::_isRegexpMatched('/\b(?:class|interface)\s+' . preg_quote($name, '/') . '\b[^{]*\{/iS', $content);
}
/**
* Analyze content to determine whether this is a direct descendant of the specified class/interface
*
* @param string $content
* @param string $name
* @return bool
*/
public static function isDirectDescendant($content, $name)
{
$name = preg_quote($name, '/');
return self::_isRegexpMatched(
'/\s+extends\s+\\\\?' . $name . '\b|\s+implements\s+[^{]*\b' . $name . '\b[^{^\\\\]*\{/iS',
$content
);
}
/**
* Check if content matches the regexp
*
* @param string $regexp
* @param string $content
* @throws \Exception
* @return bool True if the content matches the regexp
*/
protected static function _isRegexpMatched($regexp, $content)
{
$result = preg_match($regexp, $content);
if ($result === false) {
throw new \Exception('An error occurred when matching regexp "' . $regexp . '""');
}
return 1 === $result;
}
}
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility\File;
/**
* Factory for \RegexIterator
*/
class RegexIteratorFactory
{
/**
* Create instance of \RegexIterator
*
* @param string $directoryPath
* @param string $regexp
* @return \RegexIterator
*/
public function create($directoryPath, $regexp)
{
$directory = new \RecursiveDirectoryIterator($directoryPath);
$recursiveIterator = new \RecursiveIteratorIterator($directory);
return new \RegexIterator($recursiveIterator, $regexp, \RegexIterator::GET_MATCH);
}
}
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\TestFramework\Utility;
/**
* Helper class to search files by provided directory and file pattern.
*/
class FilesSearch
{
/**
* Read files from generated lists.
*
* @param string $listsBaseDir
* @param string $listFilePattern
* @param callable $noListCallback
* @return string[]
*/
public static function getFilesFromListFile(
string $listsBaseDir,
string $listFilePattern,
callable $noListCallback
): array {
$filesDefinedInList = [];
$listFiles = glob($listsBaseDir . '/_files/' . $listFilePattern);
if (!empty($listFiles)) {
foreach ($listFiles as $listFile) {
$filesDefinedInList[] = file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
$filesDefinedInList = array_merge([], ...$filesDefinedInList);
} else {
$filesDefinedInList = call_user_func($noListCallback);
}
array_walk(
$filesDefinedInList,
function (&$file) {
$file = BP . '/' . $file;
}
);
$filesDefinedInList = array_values(array_unique($filesDefinedInList));
return $filesDefinedInList;
}
}
@@ -0,0 +1,113 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Utility;
/**
* Check if one or more functions are used in the file
*/
class FunctionDetector
{
/**
* Detect functions in given file
*
* return result in this format:
* [
* line_number => [
* function_name_1,
* function_name_2,
* function_name_3,
* ],
* line_number => [
* function_name_1,
* function_name_2,
* function_name_3,
* ],
* ]
*
* @param string $filePath
* @param string[] $functions
* @return array
*/
public function detect($filePath, $functions)
{
$result = [];
$regexp = $this->composeRegexp($functions);
if (!$regexp) {
return $result;
}
$fileContent = \Magento\TestFramework\Utility\ChangedFiles::getChangedContent($filePath);
$file = file($filePath);
return $fileContent
? $this->grepChangedContent($file, $regexp, $functions, $fileContent)
: $this->grepFile($file, $regexp);
}
/**
* Grep only changed content.
*
* @param array $file
* @param string $regexp
* @param string[] $functions
* @param string $fileContent
* @return array
*/
public function grepChangedContent(array $file, $regexp, $functions, $fileContent)
{
$result = [];
$matches = preg_grep($regexp, explode("\n", $fileContent));
if (!empty($matches)) {
foreach ($matches as $line) {
$actualFunctions = [];
foreach ($functions as $function) {
if (false !== strpos($line, $function)) {
$actualFunctions[] = $function;
}
}
$result[array_search($line . "\n", $file) + 1] = $actualFunctions;
}
}
return $result;
}
/**
* Grep File.
*
* @param array $file
* @param string $regexp
* @return array
*/
public function grepFile(array $file, $regexp)
{
$result = [];
array_unshift($file, '');
$lines = preg_grep($regexp, $file);
foreach ($lines as $lineNumber => $line) {
if (preg_match_all($regexp, $line, $matches)) {
$result[$lineNumber] = $matches[1];
}
}
return $result;
}
/**
* Compose regular expression
*
* @param array $functions
* @return string
*/
private function composeRegexp(array $functions)
{
if (empty($functions)) {
return '';
}
return '/(?<!function |->|::)\b(' . join('|', $functions) . ')\s*\(/i';
}
}