public function leaveNode(PhpParser\Node $node) { // check for static method calls (ie: Things::doStuff()) if ($node instanceof Expr\StaticCall && !$this->inGlobalScope()) { $obj = new NodeWrapper($node); $name = $obj->getName(); list($className) = explode('::', $name); // only report static method calls for php classes that are // not safe for instantiation (ie: with external resources) $dictionary = new Dictionary(); if (!$dictionary->isClassSafeForInstantiation($className)) { $this->stack->addIssue(new StaticMethodCallIssue($node)); } } }
/** * @covers edsonmedina\php_testability\Dictionary::__construct * @covers edsonmedina\php_testability\Dictionary::isClassSafeForInstantiation */ public function testIsClassSafeForInstantiation() { $d = new Dictionary(); // safe classes $this->assertTrue($d->isClassSafeForInstantiation('DateTime')); $this->assertTrue($d->isClassSafeForInstantiation('RecursiveArrayIterator')); $this->assertTrue($d->isClassSafeForInstantiation('SplHeap')); // unsafe classes $this->assertFalse($d->isClassSafeForInstantiation('PDO')); $this->assertFalse($d->isClassSafeForInstantiation('SoapClient')); // user class $this->assertFalse($d->isClassSafeForInstantiation('edsonmedina\\php_testability\\Dictionary')); $this->assertFalse($d->isClassSafeForInstantiation('SomeClass')); }
public function leaveNode(PhpParser\Node $node) { // check for "new" statements if ($node instanceof Expr\New_ && !$this->inGlobalScope() && !$this->insideThrow) { $parentClass = $this->stack->findContextOfType(new CollectionSpecification()); if ($parentClass === false || stripos($parentClass->getName(), 'Factory') === FALSE) { $dictionary = new Dictionary(); $obj = new NodeWrapper($node); // only report internal php classes if not safe for // instantiation (ie: with external resources) if (!$dictionary->isClassSafeForInstantiation($obj->getName())) { $this->stack->addIssue(new NewInstanceIssue($node)); } } } elseif ($node instanceof Stmt\Throw_) { $this->insideThrow = false; } }