From 3a2fdb9389b93817aace5eec3117757d13aadbe3 Mon Sep 17 00:00:00 2001 From: Servuc Date: Tue, 2 Oct 2018 21:41:22 +0200 Subject: [PATCH] Improvement : Make PHP7 type hiting --- .gitignore | 3 +- composer.json | 1 + src/Analyser.php | 22 +++++++------- src/AnalysisResult.php | 13 ++++----- src/Command.php | 32 ++++++++++----------- src/helper/ArrayHelper.php | 2 +- src/integration/AbstractIntegration.php | 28 +++++++++++------- src/integration/PHPCodeSniffer.php | 8 +++--- src/integration/PHPCopyPasteDetector.php | 8 +++--- src/integration/PHPMessDetector.php | 8 +++--- src/output/AbstractOutput.php | 4 +-- src/output/CsvOutput.php | 2 +- src/output/HtmlOutput.php | 13 +++++---- src/output/JsonOutput.php | 2 +- src/output/TextOutput.php | 4 +-- src/output/TextTriggerTrait.php | 2 +- src/output/TriggerableInterface.php | 4 +-- src/output/XmlOutput.php | 6 ++-- src/output/filter/DiffOutputFilter.php | 10 +++---- src/output/filter/OutputFilterInterface.php | 2 +- src/output/html/FileHighlighter.php | 10 +++---- src/output/html/History.php | 14 ++++----- tests/AnalysisResultTest.php | 2 +- 23 files changed, 104 insertions(+), 96 deletions(-) diff --git a/.gitignore b/.gitignore index 612d779..02b09bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /phphound/ /build/ /vendor/ -composer.lock \ No newline at end of file +composer.lock +.idea/ \ No newline at end of file diff --git a/composer.json b/composer.json index 7fc53d4..f9fb76c 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ } ], "require": { + "ext-json":"*", "squizlabs/php_codesniffer": "2.*", "sebastian/phpcpd": "2.*", "phpmd/phpmd": "2.2.*", diff --git a/src/Analyser.php b/src/Analyser.php index bc01aa4..44504cb 100644 --- a/src/Analyser.php +++ b/src/Analyser.php @@ -54,7 +54,7 @@ class Analyser * @param string[] $analysedPaths target file or directory path. * @param string[] $ignoredPaths comma separated list of ignored directories. */ - public function __construct(AbstractOutput $output, $binariesPath, $analysedPaths, $ignoredPaths) + public function __construct(AbstractOutput $output, string $binariesPath, array $analysedPaths, array $ignoredPaths) { $this->output = $output; $this->binariesPath = $binariesPath; @@ -66,7 +66,7 @@ public function __construct(AbstractOutput $output, $binariesPath, $analysedPath * Run each configured PHP analysis tool. * @return boolean true if it didn't find code issues. */ - public function run() + public function run() : bool { $result = $this->createResult(); $this->trigger( @@ -89,7 +89,7 @@ public function run() $this->output->result($result); $this->trigger(self::EVENT_FINISHED_ANALYSIS); - return !$result->hasIssues(); + return ! $result->hasIssues(); } /** @@ -98,7 +98,7 @@ public function run() * @param string|null $message optional message. * @return void */ - protected function trigger($event, $message = null) + protected function trigger(int $event, $message = null) : void { if ($this->output instanceof TriggerableInterface) { $this->output->trigger($event, $message); @@ -109,7 +109,7 @@ protected function trigger($event, $message = null) * Get a list of paths to be ignored by the analysis. * @return string[] a list of file and/or directory paths. */ - public function getIgnoredPaths() + public function getIgnoredPaths() : array { return $this->ignoredPaths; } @@ -118,7 +118,7 @@ public function getIgnoredPaths() * Analysis target path. * @return string[] target path. */ - public function getAnalysedPaths() + public function getAnalysedPaths() : array { return $this->analysedPaths; } @@ -127,7 +127,7 @@ public function getAnalysedPaths() * Add an output filter to delegate to the analysis result object. * @param OutputFilterInterface $filter filter instance. */ - public function setResultsFilter(OutputFilterInterface $filter) + public function setResultsFilter(OutputFilterInterface $filter) : void { $this->resultsFilter = $filter; } @@ -137,7 +137,7 @@ public function setResultsFilter(OutputFilterInterface $filter) * @param string[] $paths target paths. * @return void */ - public function setAnalysedPaths(array $paths) + public function setAnalysedPaths(array $paths) : void { $this->analysedPaths = $paths; } @@ -146,7 +146,7 @@ public function setAnalysedPaths(array $paths) * List of PHP analys integration classes. * @return string[] array of class names. */ - protected function getAnalysisToolsClasses() + protected function getAnalysisToolsClasses() : string { return [ 'phphound\integration\PHPCodeSniffer', @@ -159,7 +159,7 @@ protected function getAnalysisToolsClasses() * Set of PHP analys integration objects. * @return phphound\integration\AbstractIntegration[] set of objects. */ - protected function getAnalysisTools() + protected function getAnalysisTools() : array { $objects = []; @@ -176,7 +176,7 @@ protected function getAnalysisTools() * Create an empty analysis result. * @return AnalysisResult instance. */ - protected function createResult() + protected function createResult() : AnalysisResult { return new AnalysisResult; } diff --git a/src/AnalysisResult.php b/src/AnalysisResult.php index 8d53202..8130f14 100644 --- a/src/AnalysisResult.php +++ b/src/AnalysisResult.php @@ -29,7 +29,7 @@ class AnalysisResult * @param string $message Issue description. * @return void */ - public function addIssue($fileName, $line, $toolName, $issueType, $message) + public function addIssue(string $fileName, int $line, string $toolName, string $issueType, string $message) : void { if (!isset($this->data[$fileName])) { $this->data[$fileName] = []; @@ -50,11 +50,10 @@ public function addIssue($fileName, $line, $toolName, $issueType, $message) * Check if there are any code issues. * @return boolean true if there are issues. */ - public function hasIssues() + public function hasIssues() : bool { // Required by PHP 5.4 - $array = $this->toArray(); - return !empty($array); + return ! empty($this->toArray()); } /** @@ -70,7 +69,7 @@ public function hasIssues() * * @return array result daya. */ - public function toArray() + public function toArray() : array { $data = []; @@ -93,7 +92,7 @@ public function toArray() * @param AnalysisResult $other another analysis result object. * @return AnalysisResult returns itself. */ - public function mergeWith(AnalysisResult $other) + public function mergeWith(AnalysisResult $other) : AnalysisResult { foreach ($other->toArray() as $fileName => $lines) { foreach ($lines as $line => $issues) { @@ -115,7 +114,7 @@ public function mergeWith(AnalysisResult $other) * Add an output filter to delegate to the analysis result object. * @param OutputFilterInterface $filter filter instance. */ - public function setResultsFilter(OutputFilterInterface $filter) + public function setResultsFilter(OutputFilterInterface $filter) : void { $this->filter = $filter; } diff --git a/src/Command.php b/src/Command.php index 86ce679..0dbd56f 100644 --- a/src/Command.php +++ b/src/Command.php @@ -50,7 +50,7 @@ class Command * @param string $binariesPath Composer binaries path. * @param array $arguments command line arguments. */ - public function __construct(CLImate $climate, $binariesPath, array $arguments) + public function __construct(CLImate $climate, string $binariesPath, array $arguments) { $this->cli = $climate; $this->cli->description($this->getDescription()); @@ -66,7 +66,7 @@ public function __construct(CLImate $climate, $binariesPath, array $arguments) * Run PHP-Hound command. * @return boolean true if it didn't find code issues or ran successfully. */ - public function run() + public function run() : bool { if ($this->hasArgumentValue('help')) { $this->cli->usage(); @@ -95,7 +95,7 @@ public function run() * @param string $gitDiff git diff arguments. * @return DiffOutputFilter filter instance. */ - protected function getGitDiffFilter($gitDiff) + protected function getGitDiffFilter(string $gitDiff) : DiffOutputFilter { $analysedPaths = $this->getAnalysedPaths(); $gitPath = array_shift($analysedPaths); @@ -117,7 +117,7 @@ protected function getGitDiffFilter($gitDiff) * @throws UnexpectedValueException on invalid format value. * @return AbstractOutput */ - protected function getOutput() + protected function getOutput() : AbstractOutput { $format = $this->getOutputFormat(); $formatClasses = $this->getOutputFormatClasses(); @@ -140,7 +140,7 @@ protected function getOutput() * Command line arguments list for CLImate. * @return array CLI list of arguments. */ - protected function getArguments() + protected function getArguments() : array { return [ 'help' => [ @@ -187,7 +187,7 @@ protected function getArguments() * Get a list of paths to be ignored by the analysis. * @return string[] a list of file and/or directory paths. */ - public function getIgnoredPaths() + public function getIgnoredPaths() : array { $ignoredArgument = $this->getArgumentValue('ignore'); $ignoredPaths = explode(',', $ignoredArgument); @@ -199,7 +199,7 @@ public function getIgnoredPaths() * @param string $pathsString the path argument value. * @return void */ - protected function setAnalysedPathsFromString($pathsString) + protected function setAnalysedPathsFromString(string $pathsString) : void { $rawAnalysedPaths = explode(',', $pathsString); $analysedPaths = array_filter($rawAnalysedPaths); @@ -211,7 +211,7 @@ protected function setAnalysedPathsFromString($pathsString) * @param string[] $paths target paths. * @return void */ - protected function setAnalysedPaths(array $paths) + protected function setAnalysedPaths(array $paths) : void { foreach ($paths as &$path) { if (0 === strpos($path, DIRECTORY_SEPARATOR)) { @@ -226,7 +226,7 @@ protected function setAnalysedPaths(array $paths) * Analysis target paths. * @return string[] a list of analysed paths (usually just one). */ - public function getAnalysedPaths() + public function getAnalysedPaths() : array { return $this->analysedPaths; } @@ -235,7 +235,7 @@ public function getAnalysedPaths() * Running script path. * @return string current script directory. */ - public function getWorkingDirectory() + public function getWorkingDirectory() : string { return getcwd(); } @@ -244,7 +244,7 @@ public function getWorkingDirectory() * Output format. * @return string format type. */ - public function getOutputFormat() + public function getOutputFormat() : string { return $this->getArgumentValue('format'); } @@ -253,7 +253,7 @@ public function getOutputFormat() * CLI output description. * @return string description. */ - public function getDescription() + public function getDescription() : string { return 'PHP Hound ' . Analyser::VERSION; } @@ -262,7 +262,7 @@ public function getDescription() * Analyser instance. * @return Analyser instance. */ - public function getAnalyser() + public function getAnalyser() : Analyser { if (null === $this->analyser) { $this->analyser = new Analyser( @@ -279,7 +279,7 @@ public function getAnalyser() * List of output format classes. * @return array array where the key is a format and its value the class. */ - protected function getOutputFormatClasses() + protected function getOutputFormatClasses() : array { return [ 'text' => 'phphound\output\TextOutput', @@ -295,7 +295,7 @@ protected function getOutputFormatClasses() * @param string $name argument name. * @return Mixed argument value. */ - protected function getArgumentValue($name) + protected function getArgumentValue(string $name) { return $this->cli->arguments->get($name); } @@ -305,7 +305,7 @@ protected function getArgumentValue($name) * @param string $name argument name. * @return boolean if the argument has informed or not. */ - protected function hasArgumentValue($name) + protected function hasArgumentValue(string $name) : bool { return $this->cli->arguments->defined($name, $this->arguments); } diff --git a/src/helper/ArrayHelper.php b/src/helper/ArrayHelper.php index e5a39e7..391af59 100644 --- a/src/helper/ArrayHelper.php +++ b/src/helper/ArrayHelper.php @@ -11,7 +11,7 @@ class ArrayHelper * @param Mixed $value value to be verified. * @return array value itself or an empty array. */ - public static function ensure($value) + public static function ensure($value) : array { if (empty($value) || !is_array($value)) { return []; diff --git a/src/integration/AbstractIntegration.php b/src/integration/AbstractIntegration.php index abcd650..9a04a95 100644 --- a/src/integration/AbstractIntegration.php +++ b/src/integration/AbstractIntegration.php @@ -38,7 +38,7 @@ abstract class AbstractIntegration * @param string $binariesPath where the bin scripts are located. * @param string $temporaryDirPath where temporary files will be created. */ - public function __construct($binariesPath, $temporaryDirPath) + public function __construct(string $binariesPath, string $temporaryDirPath) { $this->binariesPath = $binariesPath; $this->temporaryFilePath = tempnam($temporaryDirPath, 'PHP-Hound'); @@ -51,7 +51,7 @@ public function __construct($binariesPath, $temporaryDirPath) * @param array $targets target file or directory paths to be ignored. * @return void */ - public function setIgnoredPaths(array $targets) + public function setIgnoredPaths(array $targets) : void { $this->ignoredPaths = $targets; } @@ -60,7 +60,7 @@ public function setIgnoredPaths(array $targets) * Analysis results for this integration. * @return AnalysisResult analysis result object. */ - public function getAnalysisResult() + public function getAnalysisResult() : AnalysisResult { return $this->result; } @@ -68,9 +68,9 @@ public function getAnalysisResult() /** * Creates and execute tool command, returning output results. * @param string[] $targetPaths file/directory paths to be analysed. - * @return string CLI JSON output. + * @return void CLI JSON output. */ - public function run($targetPaths) + public function run(array $targetPaths) : void { $this->executeCommand($targetPaths); $this->processResults(); @@ -81,7 +81,7 @@ public function run($targetPaths) * @param string[] $targetPaths file/directory paths to be analysed. * @return void */ - protected function executeCommand($targetPaths) + protected function executeCommand(array $targetPaths) : void { exec($this->getCommand($targetPaths)); } @@ -90,7 +90,7 @@ protected function executeCommand($targetPaths) * Convert tool output into PHP Hound array output. * @return void */ - protected function processResults() + protected function processResults() : void { $content = $this->getOutputContent(); if (empty($content)) { @@ -105,7 +105,7 @@ protected function processResults() * Tool raw output. * @return string raw output contents. */ - protected function getOutputContent() + protected function getOutputContent() : string { return file_get_contents($this->temporaryFilePath); } @@ -114,19 +114,25 @@ protected function getOutputContent() * Integration description. * @return string description. */ - abstract public function getDescription(); + abstract public function getDescription() : string; /** * Create integration command to be run on the shell. * @param string[] $targetPaths file/directory paths to be analysed. * @return string shell command. */ - abstract public function getCommand($targetPaths); + abstract public function getCommand(array $targetPaths) : string; + + /** + * Generate ignore arguments for command + * @return string Ignore arguments for command + */ + abstract public function getIgnoredArgument() : string; /** * Read issues from the XML output. * @param Reader $xml XML reader object. * @return void */ - abstract protected function addIssuesFromXml(Reader $xml); + abstract protected function addIssuesFromXml(Reader $xml) : void; } diff --git a/src/integration/PHPCodeSniffer.php b/src/integration/PHPCodeSniffer.php index 3cb8fe4..7f698c8 100644 --- a/src/integration/PHPCodeSniffer.php +++ b/src/integration/PHPCodeSniffer.php @@ -14,7 +14,7 @@ class PHPCodeSniffer extends AbstractIntegration /** * @inheritdoc */ - public function getDescription() + public function getDescription() : string { return 'PHPCodeSniffer'; } @@ -22,7 +22,7 @@ public function getDescription() /** * @inheritdoc */ - public function getIgnoredArgument() + public function getIgnoredArgument() : string { if (!empty($this->ignoredPaths)) { return '--ignore=' . implode(',', $this->ignoredPaths) . ' '; @@ -33,7 +33,7 @@ public function getIgnoredArgument() /** * @inheritdoc */ - public function getCommand($targetPaths) + public function getCommand(array $targetPaths) : string { return $this->binariesPath . 'phpcs -p --standard=PSR2 --report=xml ' . $this->getIgnoredArgument() . '--report-file="' @@ -43,7 +43,7 @@ public function getCommand($targetPaths) /** * @inheritdoc */ - protected function addIssuesFromXml(Reader $xml) + protected function addIssuesFromXml(Reader $xml) : void { $xmlArray = $xml->parse(); diff --git a/src/integration/PHPCopyPasteDetector.php b/src/integration/PHPCopyPasteDetector.php index d71706d..f43a21f 100644 --- a/src/integration/PHPCopyPasteDetector.php +++ b/src/integration/PHPCopyPasteDetector.php @@ -14,7 +14,7 @@ class PHPCopyPasteDetector extends AbstractIntegration /** * @inheritdoc */ - public function getDescription() + public function getDescription() : string { return 'PHPCopyPasteDetector'; } @@ -22,7 +22,7 @@ public function getDescription() /** * @inheritdoc */ - public function getIgnoredArgument() + public function getIgnoredArgument() : string { if (!empty($this->ignoredPaths)) { return '--exclude={' . implode(',', $this->ignoredPaths) . '} '; @@ -33,7 +33,7 @@ public function getIgnoredArgument() /** * @inheritdoc */ - public function getCommand($targetPaths) + public function getCommand(array $targetPaths) : string { return $this->binariesPath . 'phpcpd ' . implode(' ', $targetPaths) . ' ' @@ -44,7 +44,7 @@ public function getCommand($targetPaths) /** * @inheritdoc */ - protected function addIssuesFromXml(Reader $xml) + protected function addIssuesFromXml(Reader $xml) : void { $xmlArray = $xml->parse(); diff --git a/src/integration/PHPMessDetector.php b/src/integration/PHPMessDetector.php index 6003730..be1a8bd 100644 --- a/src/integration/PHPMessDetector.php +++ b/src/integration/PHPMessDetector.php @@ -14,7 +14,7 @@ class PHPMessDetector extends AbstractIntegration /** * @inheritdoc */ - public function getDescription() + public function getDescription() : string { return 'PHPMessDetector'; } @@ -22,7 +22,7 @@ public function getDescription() /** * @inheritdoc */ - public function getIgnoredArgument() + public function getIgnoredArgument() : string { if (!empty($this->ignoredPaths)) { return '--exclude ' . implode(',', $this->ignoredPaths) . ' '; @@ -33,7 +33,7 @@ public function getIgnoredArgument() /** * @inheritdoc */ - public function getCommand($targetPaths) + public function getCommand(array $targetPaths) : string { return $this->binariesPath . 'phpmd ' . implode(',', $targetPaths) . ' ' . 'xml cleancode,codesize,controversial,design,naming,unusedcode ' @@ -44,7 +44,7 @@ public function getCommand($targetPaths) /** * @inheritdoc */ - protected function addIssuesFromXml(Reader $xml) + protected function addIssuesFromXml(Reader $xml) : void { $xmlArray = $xml->parse(); diff --git a/src/output/AbstractOutput.php b/src/output/AbstractOutput.php index af398ae..6a619d7 100644 --- a/src/output/AbstractOutput.php +++ b/src/output/AbstractOutput.php @@ -23,7 +23,7 @@ abstract class AbstractOutput * @param CLImate $climate CLImate instance. * @param string $outputDirectory directory where files will be created. */ - public function __construct(CLImate $climate, $outputDirectory) + public function __construct(CLImate $climate, string $outputDirectory) { $this->cli = $climate; $this->outputDirectory = $outputDirectory; @@ -34,5 +34,5 @@ public function __construct(CLImate $climate, $outputDirectory) * @param AnalysisResult $result reduced result data. * @return void */ - abstract public function result(AnalysisResult $result); + abstract public function result(AnalysisResult $result) : void; } diff --git a/src/output/CsvOutput.php b/src/output/CsvOutput.php index 60166fd..9dcc3cc 100644 --- a/src/output/CsvOutput.php +++ b/src/output/CsvOutput.php @@ -9,7 +9,7 @@ class CsvOutput extends AbstractOutput /** * @inheritdoc */ - public function result(AnalysisResult $result) + public function result(AnalysisResult $result) : void { $writer = Writer::createFromString(''); $writer->insertOne(['File', 'Line', 'Tool', 'Type', 'Message']); diff --git a/src/output/HtmlOutput.php b/src/output/HtmlOutput.php index d432a2f..0f56d6a 100644 --- a/src/output/HtmlOutput.php +++ b/src/output/HtmlOutput.php @@ -22,7 +22,7 @@ class HtmlOutput extends AbstractOutput implements TriggerableInterface /** * @inheritdoc */ - public function result(AnalysisResult $result) + public function result(AnalysisResult $result) : void { $this->cli->br(); $this->cli->inline('Writing HTML report in "./phphound/"... '); @@ -39,9 +39,10 @@ public function result(AnalysisResult $result) /** * Create HTML report index page. * @param AnalysisResult $result result data object. + * @param History $history * @return void */ - protected function writeIndexHtml(AnalysisResult $result, History $history) + protected function writeIndexHtml(AnalysisResult $result, History $history) : void { $files = []; @@ -75,7 +76,7 @@ protected function writeIndexHtml(AnalysisResult $result, History $history) * @param array $lines lines with their issues. * @return void */ - protected function writeFileHtml($phpFilePath, $lines) + protected function writeFileHtml(string $phpFilePath, array $lines) : void { $highlighter = new FileHighlighter($phpFilePath, $lines); $fileHtml = $this->renderView( @@ -100,7 +101,7 @@ protected function writeFileHtml($phpFilePath, $lines) * @param array $data variables required by view file. * @return string output HTML. */ - protected function renderView($view, $data) + protected function renderView(string $view, array $data) : string { $date = new DateTime; $content = $this->getPlatesEngine()->render($view, $data); @@ -121,7 +122,7 @@ protected function renderView($view, $data) * Configure and return Plates engine. * @return Engine Plates engine instance. */ - protected function getPlatesEngine() + protected function getPlatesEngine() : Engine { if (null === $this->platesEngine) { $this->platesEngine = new Engine(__DIR__ . '/../templates'); @@ -134,7 +135,7 @@ protected function getPlatesEngine() * Get output directory. * @return string output directory path. */ - protected function getOutputDirectory() + protected function getOutputDirectory() : string { $directory = $this->outputDirectory . '/phphound'; diff --git a/src/output/JsonOutput.php b/src/output/JsonOutput.php index a270bcf..9f74003 100644 --- a/src/output/JsonOutput.php +++ b/src/output/JsonOutput.php @@ -8,7 +8,7 @@ class JsonOutput extends AbstractOutput /** * @inheritdoc */ - public function result(AnalysisResult $result) + public function result(AnalysisResult $result) : void { $this->cli->out(json_encode($result->toArray())); } diff --git a/src/output/TextOutput.php b/src/output/TextOutput.php index 604e4ac..9a4e61a 100644 --- a/src/output/TextOutput.php +++ b/src/output/TextOutput.php @@ -10,7 +10,7 @@ class TextOutput extends AbstractOutput implements TriggerableInterface /** * @inheritdoc */ - public function result(AnalysisResult $result) + public function result(AnalysisResult $result) : void { foreach ($result->toArray() as $fileName => $lines) { $this->cli->br(); @@ -39,7 +39,7 @@ public function result(AnalysisResult $result) * @param array[] $file * @return integer number of issues in all lines */ - private function countIssues($file) + private function countIssues(array $file) : int { $counter = 0; diff --git a/src/output/TextTriggerTrait.php b/src/output/TextTriggerTrait.php index b9b9cc4..5383f4e 100644 --- a/src/output/TextTriggerTrait.php +++ b/src/output/TextTriggerTrait.php @@ -15,7 +15,7 @@ trait TextTriggerTrait * @param string|null $message optional message. * @return void */ - public function trigger($eventType, $message = null) + public function trigger(int $eventType, $message = null) : void { switch ($eventType) { case Analyser::EVENT_STARTING_ANALYSIS: diff --git a/src/output/TriggerableInterface.php b/src/output/TriggerableInterface.php index 85d5b4d..0cceb72 100644 --- a/src/output/TriggerableInterface.php +++ b/src/output/TriggerableInterface.php @@ -8,9 +8,9 @@ interface TriggerableInterface { /** * Output event messages. - * @param integer $eventType Analyser class event constant. + * @param int $eventType Analyser class event constant. * @param mixed $data Optional message. * @return void */ - public function trigger($eventType, $data = null); + public function trigger(int $eventType, $data = null) : void; } diff --git a/src/output/XmlOutput.php b/src/output/XmlOutput.php index fd0b77f..115be05 100644 --- a/src/output/XmlOutput.php +++ b/src/output/XmlOutput.php @@ -9,7 +9,7 @@ class XmlOutput extends AbstractOutput /** * @inheritdoc */ - public function result(AnalysisResult $result) + public function result(AnalysisResult $result) : void { $this->cli->out($this->getXmlFor($result)); } @@ -19,7 +19,7 @@ public function result(AnalysisResult $result) * @param AnalysisResult $result analysis result object. * @return string XML contents. */ - protected function getXmlFor(AnalysisResult $result) + protected function getXmlFor(AnalysisResult $result) : string { $writer = new Writer; $writer->openMemory(); @@ -32,7 +32,7 @@ protected function getXmlFor(AnalysisResult $result) * @param AnalysisResult $result analysis result object. * @return array XML following Sabre structure. */ - protected function getSabreXmlArrayFor(AnalysisResult $result) + protected function getSabreXmlArrayFor(AnalysisResult $result) : array { $sabreXmlArray = [ 'phphound' => [], diff --git a/src/output/filter/DiffOutputFilter.php b/src/output/filter/DiffOutputFilter.php index 44e4e2a..169eec9 100644 --- a/src/output/filter/DiffOutputFilter.php +++ b/src/output/filter/DiffOutputFilter.php @@ -28,7 +28,7 @@ class DiffOutputFilter implements OutputFilterInterface * @param string $root root path. * @param SebastianBergmann\Diff\Diff[] $diffs array of diff objects. */ - public function __construct($root, array $diffs) + public function __construct(string $root, array $diffs) { $this->root = $root; $this->diffs = $diffs; @@ -37,7 +37,7 @@ public function __construct($root, array $diffs) /** * @inheritdoc */ - public function filter($data) + public function filter(array $data) : array { $filteredData = []; @@ -62,7 +62,7 @@ public function filter($data) * Files touched by the diff and that received at least one new line of code. * @return string[] files paths. */ - public function getFilesWithAddedCode() + public function getFilesWithAddedCode() : array { $files = []; foreach ($this->getDiffsWithAddedCode() as $fileDiff) { @@ -75,7 +75,7 @@ public function getFilesWithAddedCode() * Gets the list of files and lines touched by the diff. * @return array where the key is the file path and its values the lines. */ - protected function getTouchedFilesAndLines() + protected function getTouchedFilesAndLines() : array { $resultFilter = []; foreach ($this->getDiffsWithAddedCode() as $fileDiff) { @@ -104,7 +104,7 @@ protected function getTouchedFilesAndLines() * Search for diffs where at least one line of code was added. * @return SebastianBergmann\Diff\Diff[] diffs adding code. */ - protected function getDiffsWithAddedCode() + protected function getDiffsWithAddedCode() : array { $diffs = []; foreach ($this->diffs as $fileDiff) { diff --git a/src/output/filter/OutputFilterInterface.php b/src/output/filter/OutputFilterInterface.php index 99fc207..fc257da 100644 --- a/src/output/filter/OutputFilterInterface.php +++ b/src/output/filter/OutputFilterInterface.php @@ -11,5 +11,5 @@ interface OutputFilterInterface * @param $data array a list of the file paths and their issues. * @return array filtered data array. */ - public function filter($data); + public function filter(array $data) : array; } diff --git a/src/output/html/FileHighlighter.php b/src/output/html/FileHighlighter.php index e8c7776..bb83144 100644 --- a/src/output/html/FileHighlighter.php +++ b/src/output/html/FileHighlighter.php @@ -23,7 +23,7 @@ class FileHighlighter * @param string $filePath analyzed file path. * @param array $linesWithIssues lines with respective issues. */ - public function __construct($filePath, array $linesWithIssues) + public function __construct(string $filePath, array $linesWithIssues) { $this->filePath = $filePath; $this->linesWithIssues = $linesWithIssues; @@ -33,7 +33,7 @@ public function __construct($filePath, array $linesWithIssues) * Highlight PHP file showing issues and line numbers. * @return string HTML. */ - public function getHtml() + public function getHtml() : string { $html = ""; $paddingLength = $this->getLineNumberPaddingLength(); @@ -60,7 +60,7 @@ public function getHtml() * Ammount of characters of the number of the last line of code. * @return integer padding length. */ - protected function getLineNumberPaddingLength() + protected function getLineNumberPaddingLength() : int { $lines = $this->getFormattedPHPFileLines(); $lineCount = count($lines); @@ -71,7 +71,7 @@ protected function getLineNumberPaddingLength() * Split all formatted PHP lines of code into an array. * @return string[] HTML splitted into an array. */ - protected function getFormattedPHPFileLines() + protected function getFormattedPHPFileLines() : array { $code = substr(highlight_file($this->filePath, true), 36, -15); return explode('
', $code); @@ -82,7 +82,7 @@ protected function getFormattedPHPFileLines() * @param integer $lineNumber line number. * @return string HTML. */ - protected function getIssuesTooltip($lineNumber) + protected function getIssuesTooltip(int $lineNumber) : string { if (!isset($this->linesWithIssues[$lineNumber])) { return ''; diff --git a/src/output/html/History.php b/src/output/html/History.php index 4b268ec..ab6338e 100644 --- a/src/output/html/History.php +++ b/src/output/html/History.php @@ -25,7 +25,7 @@ class History * Set dependencies. * @param string $outputDirectory target directory path. */ - public function __construct($outputDirectory) + public function __construct(string $outputDirectory) { $this->outputDirectory = $outputDirectory; } @@ -35,7 +35,7 @@ public function __construct($outputDirectory) * @param AnalysisResult $result analysis result. * @return void */ - public function append(AnalysisResult $result) + public function append(AnalysisResult $result) : void { $data = $this->getData(); @@ -72,7 +72,7 @@ public function append(AnalysisResult $result) * Stores history data. * @return boolean true if successfully wrote to the JSON file. */ - public function save() + public function save() : bool { $file = new SplFileObject($this->getHistoryFilePath(), 'w'); return $file->fwrite(json_encode($this->getData())); @@ -82,7 +82,7 @@ public function save() * Loads history data from the JSON file (or the cache if already did). * @return array */ - public function getData() + public function getData() : array { if (null === $this->cachedData) { $data = $this->getHistoryFileContent(); @@ -101,7 +101,7 @@ public function getData() * Overwrite current data. * @param array $data history data. */ - protected function setData(array $data) + protected function setData(array $data) : void { $this->cachedData = $data; } @@ -110,7 +110,7 @@ protected function setData(array $data) * History data * @return array */ - protected function getHistoryFileContent() + protected function getHistoryFileContent() : array { if (!file_exists($this->getHistoryFilePath())) { return null; @@ -123,7 +123,7 @@ protected function getHistoryFileContent() * JSON data file path. * @return string file path. */ - protected function getHistoryFilePath() + protected function getHistoryFilePath() : string { return $this->outputDirectory . '/history.json'; } diff --git a/tests/AnalysisResultTest.php b/tests/AnalysisResultTest.php index d8e7f22..455b0d2 100644 --- a/tests/AnalysisResultTest.php +++ b/tests/AnalysisResultTest.php @@ -145,7 +145,7 @@ function it_filter_results() class FakeFilter implements OutputFilterInterface { - public function filter($data) + public function filter(array $data) : array { return ['filtered', 'data']; }