public function test() { eval('function hook_tokens($type, $tokens, array $data = array(), array $options = array()) {}'); $config = ['hook' => 'tokens', 'module' => 'system']; $module_handler = $this->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface'); $plugin = new ImplementHook($config, uniqID(), [], $module_handler); $plugin->setTarget($this->target); $plugin->execute(); $module = $this->target->getPath('.module'); $function = $this->target->open($module)->children(Filter::isFunction('foo_tokens'))->get(0); $this->assertInstanceOf('\\Pharborist\\Functions\\FunctionDeclarationNode', $function); $this->assertEquals('foo_tokens', $function->getName()->getText()); $parameters = $function->getParameters(); $this->assertCount(4, $parameters); $this->assertNull($parameters[0]->getTypeHint()); $this->assertEquals('type', $parameters[0]->getName()); $this->assertNull($parameters[0]->getValue()); $this->assertNull($parameters[1]->getTypeHint()); $this->assertEquals('tokens', $parameters[1]->getName()); $this->assertNull($parameters[1]->getValue()); $this->assertInstanceOf('\\Pharborist\\TokenNode', $parameters[2]->getTypeHint()); $this->assertSame(T_ARRAY, $parameters[2]->getTypeHint()->getType()); $this->assertEquals('data', $parameters[2]->getName()); $this->assertInstanceOf('\\Pharborist\\Types\\ArrayNode', $parameters[2]->getValue()); $this->assertInstanceOf('\\Pharborist\\TokenNode', $parameters[3]->getTypeHint()); $this->assertSame(T_ARRAY, $parameters[3]->getTypeHint()->getType()); $this->assertEquals('options', $parameters[3]->getName()); $this->assertInstanceOf('\\Pharborist\\Types\\ArrayNode', $parameters[3]->getValue()); }
/** * {@inheritdoc} */ public function analyze(TargetInterface $target) { $violations = []; $indexer = $target->getIndexer('function'); if ($indexer->has('hook_form_alter')) { $violations[] = $indexer->get('hook_form_alter'); } $id = $target->id() . '_form_%_alter'; // Until kernel tests are run in PHPUnit, we need to check for // the existence of db_like(). if (function_exists('db_like')) { $id = db_like($id); } $alter_hooks = $target->getIndexer('function')->getQuery()->condition('id', $id, 'LIKE')->execute(); foreach ($alter_hooks as $alter_hook) { $violations[] = $target->open($alter_hook->file)->find(Filter::isFunction($alter_hook->id)); } $issues = []; if ($violations) { $issue = $this->buildIssue($target); array_walk($violations, function (FunctionDeclarationNode $function) use($issue) { $issue->addViolation($function, $this); }); $issues[] = $issue; } return $issues; }
public function testViolationsAndDetectors() { $analyzer = $this->getMockBuilder('\\Drupal\\drupalmoduleupgrader\\AnalyzerBase')->disableOriginalConstructor()->getMock(); $analyzer->method('getPluginId')->willReturn('blarg'); $this->issue->addAffectedFile($this->dir->getChild('foo.info')->url(), $analyzer); $code = <<<'END' <?php /** * Implements hook_permission(). */ function foo_permission() { return array(); } END; $this->dir->getChild('foo.module')->setContent($code); $node = $this->target->open($this->dir->getChild('foo.module')->url())->children(Filter::isFunction('foo_permission'))->get(0); $this->issue->addViolation($node, $analyzer); $violations = $this->issue->getViolations(); $this->assertInternalType('array', $violations); $this->assertCount(2, $violations); $this->assertArrayHasKey('file', $violations[0]); $this->assertArrayNotHasKey('line_number', $violations[0]); $this->assertEquals($this->dir->getChild('foo.info')->url(), $violations[0]['file']); $this->assertArrayHasKey('file', $violations[1]); $this->assertArrayHasKey('line_number', $violations[1]); $this->assertEquals($this->dir->getChild('foo.module')->url(), $violations[1]['file']); $detectors = $this->issue->getDetectors(); $this->assertInternalType('array', $detectors); $this->assertCount(1, $detectors); $this->assertEquals($analyzer->getPluginId(), $detectors[0]); }
/** * @requires PHP 5.6 */ public function testIsVariadic() { $doc = <<<'END' <?php function foo($a, $b, ...$c) { } END; /** @var \Pharborist\Functions\FunctionDeclarationNode $func */ $func = Parser::parseSource($doc)->children(Filter::isFunction('foo'))->get(0); $this->assertTrue($func->isVariadic()); $doc = <<<'END' <?php function baz($a, $b, $c) { } END; $func = Parser::parseSource($doc)->children(Filter::isFunction('baz'))->get(0); $this->assertFalse($func->isVariadic()); }
/** * {@inheritdoc} */ public function get($identifier) { $identifier = $this->prepareID($identifier); $file = $this->getQuery(['file'])->condition('id', $identifier)->execute()->fetchField(); return $this->target->open($file)->children(Filter::isFunction($identifier))->get(0); }