/** * @inheritdoc */ public function check(\SplFileInfo $file, Tokens $tokens) { $tokenCount = $tokens->count(); for ($index = 0; $index < $tokenCount; ++$index) { $token = $tokens[$index]; if (!$token->isGivenKind(T_CLASS)) { continue; } $prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)]; $classStart = $tokens->getNextTokenOfKind($index, array('{')); $classEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $classStart); // ignore class if it is abstract or already final if ($prevToken->isGivenKind(array(T_ABSTRACT, T_FINAL))) { $index = $classEnd; continue; } $classNameIndex = $tokens->getNextTokenOfKind($index, [[T_STRING]]); $className = $tokens[$classNameIndex]->getContent(); $tokens->reportAt($index, new Message(E_ERROR, 'check_class_should_be_final', ['class' => $className])); $index = $classEnd; } }
private function checkMethodBody(Tokens $tokens, $methodName, $methodStart, $methodEnd) { for ($index = $methodStart; $index < $methodEnd; ++$index) { if (!$tokens[$index]->isGivenKind(T_STRING) || !in_array($tokens[$index]->getContent(), ['func_get_args', 'func_get_arg', 'func_num_args'], true)) { continue; } $tokens->reportAt($index, new Message(E_ERROR, 'check_method_arguments_called_not_allowed', ['method' => $methodName, 'function' => $tokens[$index]->getContent()])); break; } }
private function checkMethod(Tokens $tokens, $index, $attributes) { if ($attributes['name'] !== '__construct') { $tokens->reportAt($index, new Message(E_ERROR, 'check_class_must_not_contain_method', ['method' => $attributes['name']])); return false; } if ($attributes['visibility'] === null) { $tokens->reportAt($index, new Message(E_ERROR, 'check_class_method_must_have_a_visibility', ['method' => $attributes['name']])); } elseif ($attributes['visibility'] !== T_PUBLIC) { $tokens->reportAt($index, new Message(E_ERROR, 'check_class_method_must_be_public', ['method' => $attributes['name'], 'visibility' => $attributes['visibility'] === T_PRIVATE ? 'private' : 'protected'])); } $functionArguments = TokensHelper::getFunctionArguments($tokens, $index); // Ensure that all arguments are used $startBodyIndex = $tokens->getNextTokenOfKind($index, ['{']); $endBodyIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startBodyIndex, true); $usedVariables = []; for ($index = $endBodyIndex - 1; $index > $startBodyIndex; --$index) { $token = $tokens[$index]; if (!$token->isGivenKind(T_VARIABLE) || $tokens[$index - 1]->isGivenKind(T_OBJECT_OPERATOR) || $tokens[$index + 1]->isGivenKind(T_OBJECT_OPERATOR)) { continue; } $usedVariables[$index] = $token->getContent(); } // Report unused arguments $unusedArguments = array_diff(array_map(function ($info) { return $info['token']->getContent(); }, $functionArguments), $usedVariables); foreach ($unusedArguments as $argumentIndex => $argument) { $tokens->reportAt($argumentIndex, new Message(E_ERROR, 'check_class_method_argument_not_used', ['method' => $attributes['name'], 'argument' => $argument])); } return ['arguments' => $functionArguments]; }
/** * @param \SplFileInfo $file * @param Tokens $tokens * @param $namespace * @param $namespaceIndex */ public function checkNamespaceDirectory(\SplFileInfo $file, Tokens $tokens, $namespace, $namespaceIndex) { if ($namespace === null) { return; } $path = trim(preg_replace('#/+#', '/', $file->getPath()), '/'); $parts = []; while (!isset($this->dirMap[$path])) { $pathPos = strrpos($path, '/'); if ($pathPos === false) { return; } $parts[] = substr($path, $pathPos + 1); $path = substr($path, 0, $pathPos); } if (!isset($this->dirMap[$path])) { return; } $expectedNamespace = $this->dirMap[$path] . (empty($parts) ? '' : '\\' . implode('\\', array_reverse($parts))); if ($expectedNamespace !== $namespace) { $tokens->reportAt($namespaceIndex, new Message(E_ERROR, 'check_psr4_namespace_is_invalid', ['expected' => $expectedNamespace, 'namespace' => $namespace])); } }