ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
+21
@@ -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);
|
||||
}
|
||||
+86
@@ -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;
|
||||
}
|
||||
}
|
||||
+21
@@ -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);
|
||||
}
|
||||
+119
@@ -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;
|
||||
}
|
||||
}
|
||||
+82
@@ -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;
|
||||
}
|
||||
}
|
||||
+120
@@ -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];
|
||||
}
|
||||
}
|
||||
+135
@@ -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]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user