Esempio n. 1
0
 public function testWalkerCapturesAllAnalysisExceptionsAndReturnsThem()
 {
     $ast = $this->parseSource(file_get_contents(STUBPATH . '/walk/class_2.php'));
     $walker = new NodeWalker();
     $errors = $walker->walk($ast, function (Node $node) {
         throw AnalysisException::withNode($node);
     });
     $this->assertCount(9, $errors);
     $this->assertArrayOf(AnalysisException::class, $errors);
 }
 /**
  * Computes the complexity of a part of code.
  *
  * @param Node[]|Node|null $ast Abstract Syntax Tree node(s)
  * @return int The Cyclomatic Complexity Number
  */
 public function compute($ast)
 {
     $complexity = 0;
     $walker = new NodeWalker();
     // Walk through all the nodes one by one
     $walker->walk($ast, function (Node $node) use(&$complexity) {
         // Add the complexity value of the given node
         // to the overall complexity of the AST
         $complexity += $this->complexityChecker->getValue($node);
     });
     return $complexity;
 }
Esempio n. 3
0
 /**
  * @param Node[]|null $ast
  * @return array
  */
 public function analyze($ast)
 {
     $walker = new NodeWalker();
     $errors = $walker->walk($ast, function ($node) {
         foreach ($this->checkers() as $class) {
             /** @var CheckerInterface $checker */
             $checker = new $class();
             if ($checker instanceof ParametersInterface) {
                 $checker->setParameters($this->config);
             }
             $this->bootstrapper->bootstrap($checker);
             $checker->check($node);
         }
     });
     return $errors;
 }