Skip to content

Commit 51020c3

Browse files
authored
Merge pull request #4 from mnapoli/better-error-messages
Add explicit error messages when the programs cannot be found
2 parents 004cfd9 + 16ec2e8 commit 51020c3

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

pretty

+32-1
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,48 @@ $command = '';
2929

3030
// Detect which tool to run
3131
if (file_exists('phpcs.xml') || file_exists('phpcs.xml.dist')) {
32-
echo "CodeSniffer configuration file found, running CodeSniffer\n";
32+
echo "PHP CodeSniffer configuration file found, running CodeSniffer\n";
33+
assertPhpCodeSnifferIsInstalled();
3334
$command = $commands['phpcs'];
3435
} elseif (file_exists('.php_cs') || file_exists('.php_cs.dist')) {
3536
echo "PHP-CS-Fixer configuration file found, running PHP-CS-Fixer\n";
37+
assertPhpCsFixerIsInstalled();
3638
$command = $commands['php-cs-fixer'];
3739
} else {
3840
echo "No configuration file found, running PHP CodeSniffer with PSR-2\n";
41+
assertPhpCodeSnifferIsInstalled();
3942
$command = $commands['default'];
4043
}
4144

4245
// Run the analysis
4346
passthru($command, $exitCode);
4447

4548
exit($exitCode);
49+
50+
function commandExists($command)
51+
{
52+
$return = shell_exec('which ' . escapeshellarg($command));
53+
return !empty($return);
54+
}
55+
56+
function assertPhpCodeSnifferIsInstalled()
57+
{
58+
if (!commandExists('phpcs')) {
59+
echo <<<INSTRUCTIONS
60+
ERROR: PHP CodeSniffer does not seem to be installed because the 'phpcs' program cannot be found.
61+
You can install it by following the instructions here: https://github.com/squizlabs/PHP_CodeSniffer#installation
62+
INSTRUCTIONS;
63+
exit(1);
64+
}
65+
}
66+
67+
function assertPhpCsFixerIsInstalled()
68+
{
69+
if (!commandExists('php-cs-fixer')) {
70+
echo <<<INSTRUCTIONS
71+
ERROR: PHP-CS-Fixer does not seem to be installed because the 'php-cs-fixer' program cannot be found.
72+
You can install it by following the instructions here: https://github.com/FriendsOfPHP/PHP-CS-Fixer#installation
73+
INSTRUCTIONS;
74+
exit(1);
75+
}
76+
}

0 commit comments

Comments
 (0)