diff --git a/bin/super-giggle b/bin/super-giggle index 407165b..2fc4242 100755 --- a/bin/super-giggle +++ b/bin/super-giggle @@ -12,13 +12,12 @@ if (file_exists(__DIR__ . '/../vendor/autoload.php') === true) { } elseif (file_exists(__DIR__ . '/../../../autoload.php') === true) { require __DIR__ . '/../../../autoload.php'; } else { - echo 'Warning: Dependencies from composer not found. The required files...'; - require __DIR__ . '/src/Main.php'; - require __DIR__ . '/src/Util.php'; + require __DIR__ . '/../src/Main.php'; + require __DIR__ . '/../src/Util.php'; } -use SupperGiggle\Main; -use SupperGiggle\Util; +use SuperGiggle\Main; +use SuperGiggle\Util; $opts = Util::parseArgs(); if (isset($opts['help'])) { @@ -27,4 +26,3 @@ if (isset($opts['help'])) { $phpcs = new Main(); $phpcs->run($opts); - diff --git a/scripts/build-phar b/scripts/build-phar index 22e34cb..8c1208f 100755 --- a/scripts/build-phar +++ b/scripts/build-phar @@ -22,12 +22,12 @@ Phar::mapPhar("super-giggle.phar"); require_once "phar://super-giggle.phar/Main.php"; require_once "phar://super-giggle.phar/Util.php"; -$opts = SupperGiggle\Util::parseArgs(); +$opts = SuperGiggle\Util::parseArgs(); if (isset($opts["help"])) { - SupperGiggle\Util::printUsage(); + SuperGiggle\Util::printUsage(); } -$sg = new SupperGiggle\Main(); +$sg = new SuperGiggle\Main(); $sg->isPhar = true; $sg->run($opts); exit(0); diff --git a/src/Main.php b/src/Main.php index a6c6604..74502af 100644 --- a/src/Main.php +++ b/src/Main.php @@ -1,4 +1,5 @@ options['phpcs']; $warnings = $this->options['warnings']; $execString = Util::isWindows() === true ? "$phpcs --report=json --standard=$stndr $file $warnings" : - "$php $phpcs --report=json --standard=$stndr '$file' $warnings"; + "$php $phpcs --report=json --standard=$stndr '$file' $warnings"; $response = shell_exec($execString); // Some encoding issues makes PHPCS return empty object, causing invalid JSON. // This is a quick fix. $json = json_decode(str_replace('},,{', '},{', $response), true); + if (empty($json['files']) === false) { return current($json['files'])['messages']; } else { @@ -267,11 +269,13 @@ public function run(array $options = null): void $this->printError($file, $crrPhpcsError); } else { foreach ($gitChanges as $crrChange) { - if ($crrPhpcsError['line'] >= $crrChange['line'] + if ( + $crrPhpcsError['line'] >= $crrChange['line'] && $crrPhpcsError['line'] <= ($crrChange['line'] + $crrChange['range']) ) { $this->printError($file, $crrPhpcsError); - } elseif (($crrPhpcsError['line'] + 1) >= $crrChange['line'] + } elseif ( + ($crrPhpcsError['line'] + 1) >= $crrChange['line'] && $crrPhpcsError['line'] <= ($crrChange['line'] + $crrChange['range']) ) { // Check for errors right after the line changed. @@ -294,6 +298,7 @@ public function run(array $options = null): void /** * Validate all required fieldsand exit if it fails. + * TODO: split this method in a separated class. * * @return void */ @@ -316,14 +321,35 @@ private function validateOptions(): void ); } } else { - $this->exitIf( - isset($this->options['phpcs']) && empty(shell_exec("command -v $base/{$this->options['phpcs']}")), - "'phpcs' not found. Please, install it or use ``phpcs`` option to indicate the path" - ); - $this->exitIf( - (isset($this->options['phpcs']) && !file_exists("$base/vendor/squizlabs/php_codesniffer/bin/phpcs")), - "Dependency file 'phpcs' not found. Please, install it using composer or use ``--phpcs`` option to indicate the executable" - ); + if (isset($this->options['phpcs']) === true) { + $cwd = getcwd(); + $pathRegular = $this->options['phpcs']; + $pathRelative = "$cwd/{$this->options['phpcs']}"; + $pathVendor = "$base/vendor/squizlabs/php_codesniffer/bin/phpcs"; + if (empty(shell_exec("command -v $pathRegular")) === false) { + $this->options['phpcs'] = $pathRegular; + } elseif (empty(shell_exec("command -v $pathRelative")) === false) { + $this->options['phpcs'] = $pathRelative; + } elseif (file_exists($pathVendor) === true) { + $this->options['phpcs'] = $pathVendor; + } else { + $this->exit("phpcs not found.\n\nPlease, make sure the given ``--path={$this->options['phpcs']}`` points to the correct path."); + } + } else { + // It can be installed using composer, git+composer or downloaded. + if (file_exists("$base/vendor/squizlabs/php_codesniffer/bin/phpcs") === true) { + $this->options['phpcs'] = "$base/vendor/squizlabs/php_codesniffer/bin/phpcs"; + } elseif (file_exists("$base/../../squizlabs/php_codesniffer/bin/phpcs") === true) { + $this->options['phpcs'] = "$base/../../squizlabs/php_codesniffer/bin/phpcs"; + } else { + $this->options['phpcs'] = 'phpcs'; + } + + $this->exitIf( + empty(shell_exec("command -v {$this->options['phpcs']}")), + "{$this->options['phpcs']} not valid phpcs command. Please, make sure it exists." + ); + } } $this->exitIf( @@ -352,7 +378,8 @@ private function validateOptions(): void if (isset($this->options['repo']) === true) { $this->exit('Empty value for ``--repo``'); } else { - if (preg_match('#^(.+)\.git#i', shell_exec('git rev-parse --git-dir'), $result) === 1 + if ( + preg_match('#^(.+)\.git#i', shell_exec('git rev-parse --git-dir'), $result) === 1 && isset($result[1]) === true ) { $this->options['repo'] = $result[1]; diff --git a/src/Util.php b/src/Util.php index fa0ff8b..debb3d7 100644 --- a/src/Util.php +++ b/src/Util.php @@ -1,4 +1,5 @@