/** * @param \PHP_CodeSniffer_File $phpcsFile * @param integer $openTagPointer */ public function process(PHP_CodeSniffer_File $phpcsFile, $openTagPointer) { $referencedNames = ReferencedNameHelper::getAllReferencedNames($phpcsFile, $openTagPointer); $useStatements = UseStatementHelper::getUseStatements($phpcsFile, $openTagPointer); foreach ($referencedNames as $referencedName) { $pointer = $referencedName->getPointer(); $name = $referencedName->getNameAsReferencedInFile(); $normalizedName = UseStatement::normalizedNameAsReferencedInFile($name); if (isset($useStatements[$normalizedName]) && $referencedName->hasSameUseStatementType($useStatements[$normalizedName])) { $useStatement = $useStatements[$normalizedName]; if (in_array($useStatement->getFullyQualifiedTypeName(), $this->getIgnoredNames(), true) || !StringHelper::endsWith($useStatement->getFullyQualifiedTypeName(), 'Exception') && $useStatement->getFullyQualifiedTypeName() !== 'Throwable' && (!StringHelper::endsWith($useStatement->getFullyQualifiedTypeName(), 'Error') || NamespaceHelper::hasNamespace($useStatement->getFullyQualifiedTypeName())) && !in_array($useStatement->getFullyQualifiedTypeName(), $this->getSpecialExceptionNames(), true)) { continue; } } else { $fileNamespace = NamespaceHelper::findCurrentNamespaceName($phpcsFile, $pointer); $canonicalName = $name; if (!NamespaceHelper::isFullyQualifiedName($name) && $fileNamespace !== null) { $canonicalName = sprintf('%s%s%s', $fileNamespace, NamespaceHelper::NAMESPACE_SEPARATOR, $name); } if (in_array($canonicalName, $this->getIgnoredNames(), true) || !StringHelper::endsWith($name, 'Exception') && $name !== 'Throwable' && (!StringHelper::endsWith($canonicalName, 'Error') || NamespaceHelper::hasNamespace($canonicalName)) && !in_array($canonicalName, $this->getSpecialExceptionNames(), true)) { continue; } } if (!NamespaceHelper::isFullyQualifiedName($name)) { $phpcsFile->addError(sprintf('Exception %s should be referenced via a fully qualified name', $name), $pointer, self::CODE_NON_FULLY_QUALIFIED_EXCEPTION); } } }
/** * @param \PHP_CodeSniffer_File $phpcsFile * @param integer $usePointer */ public function process(PHP_CodeSniffer_File $phpcsFile, $usePointer) { if (UseStatementHelper::isAnonymousFunctionUse($phpcsFile, $usePointer) || UseStatementHelper::isTraitUse($phpcsFile, $usePointer)) { return; } $namespaceName = NamespaceHelper::findCurrentNamespaceName($phpcsFile, $usePointer); if ($namespaceName === null) { $namespaceName = ''; } $usedTypeName = UseStatementHelper::getFullyQualifiedTypeNameFromUse($phpcsFile, $usePointer); if (!StringHelper::startsWith($usedTypeName, $namespaceName)) { return; } $asPointer = $this->findAsPointer($phpcsFile, $usePointer); if ($asPointer !== null) { return; } $usedTypeNameRest = substr($usedTypeName, strlen($namespaceName)); if (!NamespaceHelper::isFullyQualifiedName($usedTypeNameRest) && $namespaceName !== '') { return; } if (!NamespaceHelper::hasNamespace($usedTypeNameRest)) { $fix = $phpcsFile->addFixableError(sprintf('Use %s is from the same namespace – that is prohibited', $usedTypeName), $usePointer, self::CODE_USE_FROM_SAME_NAMESPACE); if ($fix) { $phpcsFile->fixer->beginChangeset(); $endPointer = $phpcsFile->findNext(T_SEMICOLON, $usePointer) + 1; for ($i = $usePointer; $i <= $endPointer; $i++) { $phpcsFile->fixer->replaceToken($i, ''); } $phpcsFile->fixer->endChangeset(); } } }
/** * @param \PHP_CodeSniffer_File $phpcsFile * @param integer $openTagPointer */ public function process(PHP_CodeSniffer_File $phpcsFile, $openTagPointer) { $tokens = $phpcsFile->getTokens(); $referencedNames = ReferencedNameHelper::getAllReferencedNames($phpcsFile, $openTagPointer); foreach ($referencedNames as $referencedName) { $name = $referencedName->getNameAsReferencedInFile(); $pointer = $referencedName->getPointer(); $canonicalName = NamespaceHelper::normalizeToCanonicalName($name); if (NamespaceHelper::isFullyQualifiedName($name)) { $isExceptionByName = StringHelper::endsWith($name, 'Exception') || $name === '\\Throwable' || StringHelper::endsWith($name, 'Error') && !NamespaceHelper::hasNamespace($name) || in_array($canonicalName, $this->getSpecialExceptionNames(), true); $inIgnoredNames = in_array($canonicalName, $this->getIgnoredNames(), true); if ($this->isClassRequiredToBeUsed($name) && (!$this->allowFullyQualifiedExceptions || !$isExceptionByName || $inIgnoredNames)) { $previousKeywordPointer = TokenHelper::findPreviousExcluding($phpcsFile, array_merge(TokenHelper::$nameTokenCodes, [T_WHITESPACE, T_COMMA]), $pointer - 1); if (!in_array($tokens[$previousKeywordPointer]['code'], $this->getFullyQualifiedKeywords(), true)) { if (!NamespaceHelper::hasNamespace($name) && NamespaceHelper::findCurrentNamespaceName($phpcsFile, $pointer) === null) { $phpcsFile->addError(sprintf('Type %s should not be referenced via a fully qualified name, but via an unqualified name without the leading \\, because the file does not have a namespace and the type cannot be put in a use statement', $name), $pointer, self::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME_WITHOUT_NAMESPACE); } else { $phpcsFile->addError(sprintf('Type %s should not be referenced via a fully qualified name, but via a use statement', $name), $pointer, self::CODE_REFERENCE_VIA_FULLY_QUALIFIED_NAME); } } } } elseif (!$this->allowPartialUses) { if (NamespaceHelper::isQualifiedName($name)) { $phpcsFile->addError(sprintf('Partial use statements are not allowed, but referencing %s found', $name), $pointer, self::CODE_PARTIAL_USE); } } } }