/**
  * @param string $composer
  * @param string $lib
  * @param array $info
  * @param string $level
  * @return Result
  */
 private function createIssues($composer, $lib, array $info, $level)
 {
     $result = new Result();
     foreach ($info['advisories'] as $advisory) {
         $result->addIssue($this->createIssue($composer, $lib, $info['version'], $advisory, $level));
     }
     return $result;
 }
Example #2
0
 /**
  * @param string $path
  * @param array $config
  * @param AbstractLogger $logger
  * @return Result
  */
 private function executeGadgets($path, array $config, AbstractLogger $logger)
 {
     $result = new Result();
     foreach ($config as $type => $options) {
         $gadget = $this->repository->get($type);
         $gadgetResult = $this->executeGadget($gadget, $path, $options, $logger);
         $result->merge($gadgetResult);
     }
     return $result;
 }
Example #3
0
 /**
  * @param Result $result
  * @return string
  */
 public function format(Result $result)
 {
     $issues = ResultHelper::sortIssues($result->getIssues());
     $metrics = ResultHelper::sortMetrics($result->getMetrics());
     $markdown = new MarkdownBuilder();
     $markdown->h1(count($issues) . ' Issue(s)');
     foreach (ResultHelper::groupIssues($issues) as $file => $issues) {
         $this->renderSection($markdown, $file, $issues);
     }
     $markdown->h1(count($metrics) . ' Metric(s)');
     foreach ($metrics as $metric) {
         $this->renderMetric($markdown, $metric);
     }
     return $markdown->getMarkdown();
 }
Example #4
0
 /**
  * @param string $path
  * @param array $options
  * @param AbstractLogger $logger
  * @return Result
  */
 public function run($path, array $options, AbstractLogger $logger)
 {
     $result = new Result();
     $files = FilesystemHelper::findFiles($path, $options['files'], '*.twig');
     foreach ($files as $file) {
         try {
             $this->twig->parse($this->twig->tokenize(file_get_contents($file), $file));
         } catch (\Twig_Error $e) {
             $message = get_class($e) . ': ' . $e->getRawMessage();
             $issue = new Issue($this, $message);
             $issue->setLevel($options['error_level']);
             $issue->setFile($file);
             $issue->setLine($e->getTemplateLine());
             $result->addIssue($issue);
         }
     }
     return $result;
 }
Example #5
0
 /**
  * @param Result $result
  * @return string
  */
 public function format(Result $result)
 {
     $issues = ResultHelper::sortIssues($result->getIssues());
     $metrics = ResultHelper::sortMetrics($result->getMetrics());
     $markdown = new MarkdownBuilder();
     $markdown->h1(count($issues) . ' Issue(s)');
     $markdown->bulletedList(array_map(function (Issue $issue) {
         return sprintf('%s on line %s: %s', $issue->getFile(), $issue->getLine(), $issue->getTitle());
     }, $issues));
     $metrics = array_filter($metrics, function (Metric $metric) {
         return $metric->getValue() != 0;
     });
     $markdown->h1(count($metrics) . ' Metric(s)');
     $markdown->bulletedList(array_map(function (Metric $metric) {
         return sprintf('[%s] %s: %s', $metric->getCode(), $metric->getTitle(), $metric->getValue());
     }, $metrics));
     return $markdown->getMarkdown();
 }
Example #6
0
 /**
  * @param string $message
  * @param Node   $node
  * @param string $level
  */
 private function addIssue($message, Node $node = null, $level = Issue::LEVEL_ERROR)
 {
     $issue = new Issue($this->gadget, $message);
     $issue->setLevel($level);
     $issue->setFile($this->currentFile);
     if ($node) {
         $issue->setLine($node->getLine());
     }
     $this->result->addIssue($issue);
 }
Example #7
0
 /**
  * @param string $path
  * @param array $options
  * @param AbstractLogger $logger
  * @return Result
  */
 public function run($path, array $options, AbstractLogger $logger)
 {
     $processBuilder = new ProcessBuilder([$this->bin, '--report=csv']);
     foreach ($options['standards'] as $standard) {
         $processBuilder->add('--standard=' . $standard);
     }
     $processBuilder->add('--extensions=' . implode(',', $options['extensions']));
     foreach ($options['files'] as $file) {
         $processBuilder->add($file);
     }
     $processBuilder->setWorkingDirectory($path);
     $output = $processBuilder->run($logger);
     $rawIssues = $this->convertFromCsvToArray($output);
     $result = new Result();
     foreach ($rawIssues as $info) {
         $result->addIssue($this->createIssue($info));
     }
     return $result;
 }
Example #8
0
 /**
  * @param AnalyseResult $from
  * @param AnalyseResult $to
  * @param Result $result
  */
 private function prepareMetricDiff(AnalyseResult $from, AnalyseResult $to, Result $result)
 {
     $fromMetrics = $this->createMetricsHashMap($from->getMetrics());
     $toMetrics = $this->createMetricsHashMap($to->getMetrics());
     foreach ($toMetrics as $hash => $toMetric) {
         if (!isset($fromMetrics[$hash])) {
             continue;
         }
         $fromMetric = $fromMetrics[$hash];
         if ($fromMetric->getValue() == $toMetric->getValue()) {
             continue;
         }
         $metric = new Metric();
         $metric->from = $fromMetric;
         $metric->to = $toMetric;
         $metric->diff = $toMetric->getValue() - $fromMetric->getValue();
         $result->metricChanges[] = $metric;
     }
 }
Example #9
0
 /**
  * @param array $options
  * @param array $method
  * @param string $className
  * @param string $file
  *
  * @return Result
  */
 private function processMethod(array $options, array $method, $className, $file)
 {
     $result = new Result();
     if ($method['docblock'] === null) {
         if ($options['missing_docblock'] !== null) {
             $result->addIssue($this->issueFactory->createIssueForMissingDocblock($options, $method, $className, $file));
         }
         return $result;
     }
     $docBlock = new DocBlock($method['docblock']);
     $tagsInDocblock = $docBlock->getTags();
     $signature = new FunctionSignature($method['signature']);
     if ($options['type_missmatch'] !== null) {
         foreach ($this->differ->inDocBlockAndSignature($tagsInDocblock, $signature) as $params) {
             /** @var DocBlock\Tag\ParamTag $docBlockParam */
             $docBlockParam = $params[0];
             /** @var FunctionParameter $functionParameter */
             $functionParameter = $params[1];
             if (!$this->differ->equalTypes($functionParameter, $docBlockParam)) {
                 if (!$functionParameter->type) {
                     $result->addIssue($this->issueFactory->createIssueForMissingTypeInSignature($options, $method, $className, $file, $functionParameter));
                 } elseif (!$docBlockParam->getType()) {
                     $result->addIssue($this->issueFactory->createIssueForMissingTypeInDocBlock($options, $method, $className, $file, $docBlockParam));
                 } else {
                     $result->addIssue($this->issueFactory->createIssueForMismatchingTypes($options, $method, $className, $file, $functionParameter, $docBlockParam));
                 }
             }
         }
     }
     if ($options['obsolete_variable'] !== null) {
         foreach ($this->differ->inDocblockOnly($tagsInDocblock, $signature) as $param) {
             $result->addIssue($this->issueFactory->createIssueForObsoleteVariable($options, $method, $className, $file, $param->getContent()));
         }
     }
     if ($options['missing_variable'] !== null) {
         foreach ($this->differ->inSignatureOnly($tagsInDocblock, $signature) as $param) {
             $result->addIssue($this->issueFactory->createIssueForMissingVariable($options, $method, $className, $file, $param));
         }
     }
     return $result;
 }
Example #10
0
 /**
  * @param string $path
  * @param array $options
  * @param AbstractLogger $logger
  * @return Result
  */
 public function run($path, array $options, AbstractLogger $logger)
 {
     $processBuilder = new ProcessBuilder([$this->bin]);
     $processBuilder->add(implode(',', $options['files']));
     $processBuilder->add('xml');
     $processBuilder->add(implode(',', $options['rulesets']));
     $processBuilder->setWorkingDirectory($path);
     $output = $processBuilder->run($logger);
     $data = $this->convertFromXmlToArray($output);
     $result = new Result();
     if (!isset($data['file']) || !is_array($data['file'])) {
         return $result;
     }
     $files = isset($data['file'][0]) ? $data['file'] : [$data['file']];
     foreach ($files as $file) {
         $violations = isset($file['violation'][0]) ? $file['violation'] : [$file['violation']];
         foreach ($violations as $violation) {
             $result->addIssue($this->createIssue($file['@name'], $violation));
         }
     }
     return $result;
 }
Example #11
0
 /**
  * @param string $path
  * @param array $options
  * @param AbstractLogger $logger
  * @return Result
  */
 public function run($path, array $options, AbstractLogger $logger)
 {
     $file = tempnam('/tmp', 'pdepend_');
     $processBuilder = new ProcessBuilder([$this->bin]);
     $processBuilder->add('--summary-xml=' . $file);
     $processBuilder->add(implode(',', $options['files']));
     $processBuilder->setWorkingDirectory($path);
     $processBuilder->run($logger);
     $data = $this->convertFromXmlToArray(file_get_contents($file));
     (new Filesystem())->remove($file);
     $result = new Result();
     $add = function ($code, $title, $description = null) use($result, $data) {
         $metric = new Metric($title, strtolower('pdepend.' . $code), $data['@' . $code]);
         $metric->setDescription($description);
         $result->addMetric($metric);
     };
     $add('ahh', 'Average Hierarchy Height', 'The average of the maximum lenght from a root class to ist deepest subclass subclass');
     $add('andc', 'Average Number of Derived Classes', 'The average of direct subclasses of a class');
     $add('calls', 'Number of Method or Function Calls');
     $add('ccn', 'Cyclomatic Complexity Number');
     $add('ccn2', 'Extended Cyclomatic Complexity Number');
     $add('cloc', 'Comment Lines fo Code');
     $add('clsa', 'Number of Abstract Classes');
     $add('clsc', 'Number of Concrete Classes');
     $add('fanout', 'Number of Fanouts', 'Referenced Classes');
     $add('leafs', 'Number of Leaf Classes', '(final) classes');
     $add('lloc', 'Logical Lines Of Code');
     $add('loc', 'Lines Of Code');
     $add('maxDIT', 'Max Depth of Inheritance Tree', 'Maximum depth of inheritance');
     $add('ncloc', 'Non Comment Lines Of Code');
     $add('noc', 'Number Of Classes');
     $add('nof', 'Number Of Functions');
     $add('noi', 'Number Of Interfaces');
     $add('nom', 'Number Of Methods');
     $add('nop', 'Number of Packages');
     $add('roots', 'Number of Root Classes');
     return $result;
 }
 /**
  * @param string $filename
  * @param array $options
  * @param string $comment
  * @return Result
  */
 private function processComment($filename, array $options, $comment)
 {
     $result = new Result();
     foreach (explode("\n", $comment['content']) as $lineOffset => $line) {
         foreach ($options['blacklist'] as $blacklistedWord => $errorLevel) {
             if (stristr($line, $blacklistedWord) === false) {
                 continue;
             }
             $issue = new Issue($this, sprintf('found "%s" in a comment', $blacklistedWord));
             $issue->setLevel($errorLevel);
             $issue->setFile($filename);
             $issue->setLine($comment['line'] + $lineOffset);
             $result->addIssue($issue);
         }
     }
     return $result;
 }