/** * {@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; }
/** * {@inheritdoc} */ public function convert(TargetInterface $target) { $unit_tests = []; $test_files = $target->getIndexer('class')->getQuery(['file'])->condition('parent', 'DrupalUnitTestCase')->execute()->fetchCol(); foreach ($test_files as $test_file) { /** @var \Pharborist\Objects\Classnode[] $tests */ $tests = $target->open($test_file)->find(Filter::isInstanceOf('\\Pharborist\\Objects\\SingleInheritanceNode'))->toArray(); foreach ($tests as $test) { if ((string) $test->getExtends() === 'DrupalUnitTestCase') { $unit_tests[] = $test; } } } /** @var \Pharborist\Objects\ClassNode $unit_test */ foreach ($unit_tests as $unit_test) { $unit_test->setExtends('\\Drupal\\Tests\\UnitTestCase'); $comment_text = <<<END @FIXME Unit tests are now written for the PHPUnit framework. You will need to refactor this test in order for it to work properly. END; $comment = DocCommentNode::create($comment_text); $unit_test->setDocComment($comment); $ns = 'Drupal\\Tests\\' . $target->id() . '\\Unit'; $doc = RootNode::create($ns)->getNamespace($ns)->append($unit_test->remove()); WhitespaceNode::create("\n\n")->insertBefore($unit_test); $this->write($target, 'tests/src/Unit/' . $unit_test->getName() . '.php', "<?php\n\n{$doc}"); } }
/** * {@inheritdoc} */ public function convert(TargetInterface $target) { $this->target = $target; $mapping = ['DrupalWebTestCase' => 'convertWeb', 'AJAXTestCase' => 'convertAjax']; foreach ($mapping as $parent_class => $convert_method) { $test_files = $target->getIndexer('class')->getQuery(['file'])->condition('parent', $parent_class)->execute()->fetchCol(); foreach ($test_files as $test_file) { /** @var \Pharborist\Objects\Classnode[] $tests */ $tests = $target->open($test_file)->find(Filter::isInstanceOf('\\Pharborist\\Objects\\SingleInheritanceNode'))->toArray(); foreach ($tests as $test) { if ((string) $test->getExtends() === $parent_class) { $this->{$convert_method}($test); } } } } }
/** * Creates an empty implementation of a hook. * * @param TargetInterface $target * The target module. * @param string $hook * The hook to implement, without the hook_ prefix. * * @return \Pharborist\Functions\FunctionDeclarationNode * The hook implementation, appended to the main module file. */ protected function implement(TargetInterface $target, $hook) { $function = FunctionDeclarationNode::create($target->id() . '_' . $hook); $function->setDocComment(DocCommentNode::create('Implements hook_' . $hook . '().')); $module_file = $target->getPath('.module'); $target->open($module_file)->append($function); WhitespaceNode::create("\n")->insertBefore($function); WhitespaceNode::create("\n")->insertAfter($function); return $function; }