/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Stmt\ClassLike) { if (isset($node->namespacedName)) { $this->parentName = $node->namespacedName->toString(); } else { $this->parentName = $node->name; } } if ($node instanceof Node\Expr\MethodCall) { // skips concat method names like $twig->{'get'.ucfirst($type)}() if ($node->name instanceof Node\Expr\BinaryOp\Concat) { return; } // skips variable methods like $definition->$method if (!is_string($node->name)) { return; } $type = $node->var->getAttribute('guessedType', null); if (null !== $type) { $methodUsage = new MethodUsage($node->name, $type, $node->getLine(), false); $this->phpFileInfo->addMethodUsage($methodUsage); } } }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Stmt\ClassLike) { if ($node instanceof Node\Stmt\Class_ && $node->isAnonymous()) { return; } $this->parentName = $node->namespacedName->toString(); } if (!$this->hasDeprecatedDocComment($node)) { return; } if ($node instanceof Node\Stmt\Function_) { $this->phpFileInfo->addFunctionDeprecation(new FunctionDeprecation($node->name, $this->getDeprecatedDocComment($node))); return; } if ($node instanceof Node\Stmt\Class_) { $this->phpFileInfo->addClassDeprecation(new ClassDeprecation($this->parentName, $this->getDeprecatedDocComment($node))); return; } if ($node instanceof Node\Stmt\Interface_) { $this->phpFileInfo->addInterfaceDeprecation(new InterfaceDeprecation($this->parentName, $this->getDeprecatedDocComment($node))); return; } if ($node instanceof Node\Stmt\ClassMethod) { $this->phpFileInfo->addMethodDeprecation(new MethodDeprecation($this->parentName, $node->name, $this->getDeprecatedDocComment($node))); return; } }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Expr\New_ && $node->class instanceof Node\Name) { $classUsage = new ClassUsage($node->class->toString(), $node->getLine()); $this->phpFileInfo->addClassUsage($classUsage); } }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Param && $node->type instanceof Node\Name) { $typeHintUsage = new TypeHintUsage($node->type->toString(), $node->getLine()); $this->phpFileInfo->addTypeHintUsage($typeHintUsage); } }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Stmt\Class_ && $node->extends instanceof Node\Name) { $superTypeUsage = new SuperTypeUsage($node->extends->toString(), $node->namespacedName->toString(), $node->getLine()); $this->phpFileInfo->addSuperTypeUsage($superTypeUsage); } }
/** * {@inheritdoc} */ public function check(PhpFileInfo $phpFileInfo, RuleSet $ruleSet) { $violations = array(); foreach ($phpFileInfo->getDeprecatedLanguageUsages() as $deprecatedLanguageUsage) { $violations[] = new Violation($deprecatedLanguageUsage, $phpFileInfo, $deprecatedLanguageUsage->comment()); } return $violations; }
/** * {@inheritdoc} */ public function check(PhpFileInfo $phpFileInfo, RuleSet $ruleSet) { $violations = array(); foreach ($phpFileInfo->getFunctionUsages() as $functionUsage) { if ($ruleSet->hasFunction($functionUsage->name())) { $violations[] = new Violation($functionUsage, $phpFileInfo, $ruleSet->getFunction($functionUsage->name())->comment()); } } return $violations; }
/** * {@inheritdoc} */ public function check(PhpFileInfo $phpFileInfo) { $violations = array(); foreach ($phpFileInfo->classUsages() as $classUsage) { if ($this->ruleSet->hasClass($classUsage->name())) { $violations[] = new Violation($classUsage, $phpFileInfo, $this->ruleSet->getClass($classUsage->name())->comment()); } } return $violations; }
/** * @param PhpFileInfo $phpFileInfo * * @return PhpFileInfo */ public function parseFile(PhpFileInfo $phpFileInfo) { $nodes = $this->parse($phpFileInfo->getContents()); $nodes = $this->nameResolver->traverse($nodes); $nodes = $this->staticTraverser->traverse($nodes); foreach ($this->violationVisitors as $visitor) { $visitor->setPhpFileInfo($phpFileInfo); } $this->violationTraverser->traverse($nodes); return $phpFileInfo; }
/** * {@inheritdoc} */ public function check(PhpFileInfo $phpFileInfo, RuleSet $ruleSet) { $violations = array(); foreach ($phpFileInfo->interfaceUsages() as $interfaceUsageGroup) { foreach ($interfaceUsageGroup as $interfaceUsage) { if ($ruleSet->hasInterface($interfaceUsage->name())) { $violations[] = new Violation($interfaceUsage, $phpFileInfo, $ruleSet->getInterface($interfaceUsage->name())->comment()); } } } return $violations; }
/** * {@inheritdoc} */ public function check(PhpFileInfo $phpFileInfo, RuleSet $ruleSet) { $violations = array(); foreach ($phpFileInfo->typeHintUsages() as $typeHintUsage) { $isClass = $ruleSet->hasClass($typeHintUsage->name()); if ($isClass || $ruleSet->hasInterface($typeHintUsage->name())) { $usage = $isClass ? new ClassUsage($typeHintUsage->name(), $typeHintUsage->getLineNumber()) : new InterfaceUsage($typeHintUsage->name(), '', $typeHintUsage->getLineNumber()); $comment = $isClass ? $ruleSet->getClass($typeHintUsage->name())->comment() : $ruleSet->getInterface($typeHintUsage->name())->comment(); $violations[] = new Violation($usage, $phpFileInfo, $comment); } } return $violations; }
/** * {@inheritdoc} */ public function check(PhpFileInfo $phpFileInfo) { $violations = array(); foreach ($phpFileInfo->methodDefinitions() as $methodDefinition) { $ancestors = $this->ancestorResolver->getClassAncestors($phpFileInfo, $methodDefinition->parentName()); foreach ($ancestors as $ancestor) { if ($this->ruleSet->hasMethod($methodDefinition->name(), $ancestor)) { $violations[] = new Violation($methodDefinition, $phpFileInfo, $this->ruleSet->getMethod($methodDefinition->name(), $ancestor)->comment()); } } } return $violations; }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Stmt\ClassLike) { if (isset($node->namespacedName)) { $this->parentName = $node->namespacedName->toString(); } else { $this->parentName = $node->name; } } if ($node instanceof Node\Stmt\ClassMethod) { $methodDefinition = new MethodDefinition($node->name, $this->parentName, $node->getLine()); $this->phpFileInfo->addMethodDefinition($methodDefinition); } }
public function testVariableResolver() { $source = <<<'EOC' <?php namespace Foo; class Bar { function a() { } function b() { $this->a(); $x = new Bar1(); $x->a(); $x->b(); $c = new Bar2(); $y = $c; $f = $y; $f->a(); } } EOC; $fileInfo = PhpFileInfo::create($this->prophesize('Symfony\\Component\\Finder\\SplFileInfo')->reveal()); $contents = $this->traverseSourceAndReturnContents($source, $fileInfo); $methodUsages = $contents->methodUsages(); $this->assertCount(4, $methodUsages); $this->assertEquals(new MethodUsage('a', 'Foo\\Bar', 8, false), $methodUsages[0]); $this->assertEquals(new MethodUsage('a', 'Bar1', 10, false), $methodUsages[1]); $this->assertEquals(new MethodUsage('b', 'Bar1', 11, false), $methodUsages[2]); $this->assertEquals(new MethodUsage('a', 'Bar2', 15, false), $methodUsages[3]); }
/** * @param int $current * @param PhpFileInfo $file */ public function advance($current, PhpFileInfo $file) { if (!$this->verbose) { return; } if (1 === $current) { $format = '<info>%message%</info>' . "\n" . $this->label . ': <info>%current%</info>/<info>%max%</info>'; $this->progressBar->clear(); $this->progressBar->setFormat($format); } $message = $file->getRelativePathname(); $this->progressBar->setMessage($message); $this->progressBar->clear(); $this->progressBar->advance(); $this->progressBar->display(); }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Expr\AssignRef) { $this->phpFileInfo->addDeprecatedLanguageUsage(new DeprecatedLanguageUsage('assign by reference(&=)', 'Since PHP 5.3 use normal assignment instead.', $node->getLine())); } if ($node instanceof Node\Stmt\Class_) { $method = $node->getMethod($node->name); if ($method instanceof Node\Stmt\ClassMethod) { $this->phpFileInfo->addDeprecatedLanguageUsage(new DeprecatedLanguageUsage('PHP4 constructor', 'Since PHP 7.0, use __construct() instead.', $method->getLine())); } } if ($node instanceof Node\Arg) { if (true === $node->byRef) { $this->phpFileInfo->addDeprecatedLanguageUsage(new DeprecatedLanguageUsage('call-time pass-by-reference', 'Since PHP 5.3 and removed in PHP 5.4', $node->getLine())); } } }
/** * {@inheritdoc} */ public function check(PhpFileInfo $phpFileInfo, RuleSet $ruleSet) { $violations = array(); foreach ($phpFileInfo->methodUsages() as $methodUsage) { $className = $methodUsage->className(); if ($ruleSet->hasMethod($methodUsage->name(), $className)) { $violations[] = new Violation($methodUsage, $phpFileInfo, $ruleSet->getMethod($methodUsage->name(), $className)->comment()); } $ancestors = $this->ancestorResolver->getClassAncestors($phpFileInfo, $methodUsage->className()); foreach ($ancestors as $ancestor) { if ($ruleSet->hasMethod($methodUsage->name(), $ancestor)) { $violations[] = new Violation(new MethodUsage($methodUsage->name(), $ancestor, $methodUsage->getLineNumber(), $methodUsage->isStatic()), $phpFileInfo, $ruleSet->getMethod($methodUsage->name(), $ancestor)->comment()); } } } return $violations; }
/** * @return \Iterator */ public function getIterator() { $iterator = parent::getIterator(); $files = new \ArrayIterator(); foreach ($iterator as $file) { $file = PhpFileInfo::create($file); if (null !== $this->parser) { $this->parser->parseFile($file); } $files->append($file); } return $files; }
public function testNoNewStatement() { $source = <<<'EOC' <?php $foo = 'hello'; $bar = Bar::bazinga(); EOC; $splFileInfo = $this->prophesize('Symfony\\Component\\Finder\\SplFileInfo'); $usageCollection = $this->parsePhpFileFromStringAndTraverseWithVisitor($file = PhpFileInfo::create($splFileInfo->reveal()), $source, new FindClasses()); $this->assertEquals(array(), $usageCollection->classUsages()); }
public function testClassWithoutInterface() { $source = <<<EOC <?php namespace Foo; class Bar { } EOC; $splFileInfo = $this->prophesize('Symfony\\Component\\Finder\\SplFileInfo'); $phpFileInfo = $this->parsePhpFileFromStringAndTraverseWithVisitor($file = PhpFileInfo::create($splFileInfo->reveal()), $source, new FindInterfaces()); $this->assertEquals(array(), $phpFileInfo->interfaceUsages()); }
public function testNoNewStatement() { $source = <<<'EOC' <?php $bar = Bar::bazinga(); Logger::log('hello world'); EOC; $splFileInfo = $this->prophesize('Symfony\\Component\\Finder\\SplFileInfo'); $phpFileInfo = $this->parsePhpFileFromStringAndTraverseWithVisitor($file = PhpFileInfo::create($splFileInfo->reveal()), $source, new FindStaticMethodCalls()); $usages = $phpFileInfo->methodUsages(); $this->assertEquals(new MethodUsage('bazinga', 'Bar', 3, true), $usages[0]); $this->assertEquals(new MethodUsage('log', 'Logger', 4, true), $usages[1]); }
/** * @return \Iterator */ public function getIterator() { $iterator = parent::getIterator(); $files = new \ArrayIterator(); foreach ($iterator as $file) { $file = PhpFileInfo::create($file); if (null !== $this->parser) { try { $this->parser->parseFile($file); } catch (\PhpParser\Error $ex) { $raw = $ex->getRawMessage() . ' in file ' . $file; $ex->setRawMessage($raw); $this->parserErrors[] = $ex; } } $files->append($file); } return $files; }
public function testLambdaInClass() { $source = <<<'EOC' <?php namespace Foo; class Bar { public static function method() { function(\A $a, A $a) {}; } } EOC; $splFileInfo = $this->prophesize('Symfony\\Component\\Finder\\SplFileInfo'); $usages = $this->parseFileAndReturnClassUsages($file = PhpFileInfo::create($splFileInfo->reveal()), $source); $this->assertCount(2, $usages); $this->assertContains('A::7', $usages); $this->assertContains('Foo\\A::7', $usages); }
/** * @return \Iterator */ public function getIterator() { $iterator = parent::getIterator(); $files = new \ArrayIterator(); $total = $this->count(); $this->progressOutput->start($total); $i = 0; foreach ($iterator as $file) { $file = PhpFileInfo::create($file); try { $this->progressOutput->advance(++$i, $file); $this->parser->parseFile($file); } catch (\PhpParser\Error $ex) { $raw = $ex->getRawMessage() . ' in file ' . $file; $ex->setRawMessage($raw); $this->parserErrors[] = $ex; } $files->append($file); } $this->progressOutput->end(); return $files; }
/** * @param string $path * * @return Result */ public function parsePhpFiles($path) { $files = $this->finderFactory->createFinder()->in($path); $parsedFiles = array(); $parserErrors = array(); $this->progressOutput->start($fileCount = $files->count()); $i = 0; foreach ($files->getIterator() as $file) { $file = PhpFileInfo::create($file); try { $this->progressOutput->advance(++$i, $file); $this->parser->parseFile($file); } catch (Error $ex) { $raw = $ex->getRawMessage() . ' in file ' . $file; $ex->setRawMessage($raw); $parserErrors[] = $ex; } $parsedFiles[] = $file; } $this->progressOutput->end(); return new Result($parsedFiles, $parserErrors, $fileCount); }
/** * {@inheritdoc} */ public function enterNode(Node $node) { if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name) { $this->phpFileInfo->addFunctionUsage(new FunctionUsage($node->name->toString(), $node->getLine())); } }
public function testAddAndGetDeprecatedLanguageUsage() { $fileInfo = PhpFileInfo::create($this->prophesize('Symfony\\Component\\Finder\\SplFileInfo')->reveal()); $this->assertSame(array(), $fileInfo->getDeprecatedLanguageUsages()); $deprecatedLanguageUsage = $this->prophesize('SensioLabs\\DeprecationDetector\\FileInfo\\Usage\\DeprecatedLanguageUsage')->reveal(); $fileInfo->addDeprecatedLanguageUsage($deprecatedLanguageUsage); $this->assertSame(array($deprecatedLanguageUsage), $fileInfo->getDeprecatedLanguageUsages()); }
/** * @param $name * * @return PhpFileInfo|null */ protected function findDefinitionFileByName($name) { $namespaceParts = explode('\\', $name); $filename = array_pop($namespaceParts) . '.php'; $namespace = implode('\\', $namespaceParts); $finder = new Finder(); $finder->name($filename)->in($this->sourcePaths); $files = array(); /** @var SplFileInfo $file */ foreach ($finder as $file) { if (empty($namespace) || is_int(strpos($file->getContents(), $namespace))) { $baseFile = PhpFileInfo::create($file); $files[] = $this->container['parser.usage']->parseFile($baseFile); } } $file = current($files); if (!$file instanceof PhpFileInfo) { return; } return $file; }
/** * @param PhpFileInfo $file * * @return TableCell[] */ protected function getFileHeader(PhpFileInfo $file) { $cell = new TableCell(sprintf('<comment>%s</comment>', $file->getPathname()), array('colspan' => 3)); return array(new TableCell(), $cell); }
public function testAddAndGetMethodDefinitions() { $fileInfo = PhpFileInfo::create($this->prophesize('Symfony\\Component\\Finder\\SplFileInfo')->reveal()); $this->assertSame(array(), $fileInfo->methodDefinitions()); $methodDefinition = $this->prophesize('SensioLabs\\DeprecationDetector\\FileInfo\\MethodDefinition')->reveal(); $fileInfo->addMethodDefinition($methodDefinition); $this->assertSame(array($methodDefinition), $fileInfo->methodDefinitions()); }