From 5277f6759bcd4aa96c104ed815864130e82f8f7b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 17 Jan 2020 19:27:18 +0100 Subject: [PATCH] Verify the `installed_paths` after save As most problems reported are to do with paths not being set correctly, let's verify success within the plugin, independently of PHPCS. To that end, a new function `verifySaveSuccess()` has been added. This function takes the paths which were to be saved and compares then to the paths which are actually set in PHPCS after the save to determine whether or not the plugin was successful and changes the exit code if this is not the case. If there was a mismatch in paths and `verbose` mode is turned on, an information message will be displayed to help debugging. At this moment I don't have a test case for this new functionality, however, I imagine it will come in useful with future bug reports. --- src/Plugin.php | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index e024d152..943ce667 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -214,7 +214,7 @@ private function loadInstalledPaths() if (preg_match($regex, $output, $match) === 1) { $phpcsInstalledPaths = str_replace(self::PHPCS_CONFIG_KEY . ': ', '', $match[0]); $phpcsInstalledPaths = trim($phpcsInstalledPaths); - + if ($phpcsInstalledPaths !== '') { $this->installedPaths = explode(',', $phpcsInstalledPaths); } @@ -278,6 +278,9 @@ private function saveInstalledPaths() ); $exitCode = $this->processExecutor->execute($command, $configResult, $phpcsPath); + if ($exitCode === 0) { + $exitCode = $this->verifySaveSuccess(); + } if ($exitCode === 0) { $this->io->write($configMessage); @@ -292,6 +295,40 @@ private function saveInstalledPaths() return $exitCode; } + /** + * Verify that the paths which were expected to be saved, have been. + * + * @return int Exit code. 0 for success, 1 for failure. + */ + private function verifySaveSuccess() + { + $exitCode = 1; + $expectedPaths = $this->installedPaths; + + // Request the currently set installed paths after the save. + $this->loadInstalledPaths(); + + $registeredPaths = array_intersect($this->installedPaths, $expectedPaths); + $registeredCount = count($registeredPaths); + $expectedCount = count($expectedPaths); + + if ($expectedCount === $registeredCount) { + $exitCode = 0; + } + + if ($exitCode === 1 && $this->io->isVerbose()) { + $verificationMessage = sprintf( + "Paths to external standards found by the plugin: %s\n" + . 'Actual paths registered with PHPCS: %s', + implode(', ', $expectedPaths), + implode(', ', $this->installedPaths) + ); + $this->io->write($verificationMessage); + } + + return $exitCode; + } + /** * Get the path to the current PHP version being used. *