/dev/tests/static/framework/Magento/Sniffs/LiteralNamespaces/LiteralNamespacesSniff.php
PHP | 66 lines | 41 code | 6 blank | 19 comment | 1 complexity | fec726d2a6bdbceb9f68e209fa048a6c MD5 | raw file
- <?php
- /**
- * Copyright © 2016 Magento. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sniffs\LiteralNamespaces;
- use PHP_CodeSniffer_File;
- use PHP_CodeSniffer_Sniff;
- /**
- * Custom phpcs sniff to detect usages of literal class and interface names.
- */
- class LiteralNamespacesSniff implements PHP_CodeSniffer_Sniff
- {
- /**
- * @var string
- */
- private $literalNamespacePattern = '/^[\\\]{0,2}[A-Z][A-Za-z]+([\\\]{1,2}[A-Z][A-Za-z]+){2,}(?!\\\+)$/';
- /**
- * @var array
- */
- private $classNames = [];
- /**
- * @inheritdoc
- */
- public function register()
- {
- return [
- T_CONSTANT_ENCAPSED_STRING,
- T_DOUBLE_QUOTED_STRING,
- ];
- }
- /**
- * @inheritdoc
- */
- public function process(PHP_CodeSniffer_File $sourceFile, $stackPtr)
- {
- $tokens = $sourceFile->getTokens();
- if ($sourceFile->findPrevious(T_STRING_CONCAT, $stackPtr, $stackPtr - 3) ||
- $sourceFile->findNext(T_STRING_CONCAT, $stackPtr, $stackPtr + 3)
- ) {
- return;
- }
- $content = trim($tokens[$stackPtr]['content'], "\"'");
- if (preg_match($this->literalNamespacePattern, $content) === 1 && $this->classExists($content)) {
- $sourceFile->addError("Use ::class notation instead.", $stackPtr);
- }
- }
- /**
- * @param string $className
- * @return bool
- */
- private function classExists($className)
- {
- if (!isset($this->classNames[$className])) {
- $this->classNames[$className] = class_exists($className) || interface_exists($className);
- }
- return $this->classNames[$className];
- }
- }