Skip to content

Commit

Permalink
Merge pull request #6 from roger-sei/hotfix/wrong-namespace-n-phpcs-p…
Browse files Browse the repository at this point in the history
…ath-issue

Fixed several issues
  • Loading branch information
roger-sei committed Sep 19, 2019
2 parents ad83665 + 4410732 commit 8bf8108
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 29 deletions.
10 changes: 4 additions & 6 deletions bin/super-giggle
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -27,4 +26,3 @@ if (isset($opts['help'])) {

$phpcs = new Main();
$phpcs->run($opts);

6 changes: 3 additions & 3 deletions scripts/build-phar
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 41 additions & 14 deletions src/Main.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Main class for SuperGiggle, with auto runner option available.
*
Expand All @@ -13,9 +14,9 @@
* @link //github.com/roger-sei/SuperGiggle
*/

namespace SupperGiggle;
namespace SuperGiggle;

use SupperGiggle\Util;
use SuperGiggle\Util;

class Main
{
Expand Down Expand Up @@ -195,12 +196,13 @@ private function parsePHPCSErrors(string $file): array
$phpcs = $this->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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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
*/
Expand All @@ -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(
Expand Down Expand Up @@ -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];
Expand Down
26 changes: 20 additions & 6 deletions src/Util.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Util class for SuperGiggle
*
Expand All @@ -13,10 +14,12 @@
* @link //github.com/roger-sei/SuperGiggle
*/

namespace SupperGiggle;
namespace SuperGiggle;

class Util
{


/**
* Parse args and return in a friendly format
*
Expand All @@ -25,26 +28,28 @@ class Util
public static function parseArgs(): array
{
$alloweds = [
'commit:',
'help::',
'commit::',
'help',
'all::',
'verbose::',
'diff::',
'file::',
'php::',
'phpcs::',
'repo::',
'standard::',
'warnings::',
];

$opt = getopt('', $alloweds);
array_walk($opt, function (&$v) {
$v = $v === false ? true : $v;
array_walk($opt, function (&$value) {
$value = ($value === false) ? true : $value;
});

return $opt;
}


/**
* Print help information, in cli format
*
Expand Down Expand Up @@ -74,6 +79,7 @@ public static function printUsage(): void
exit(0);
}


/**
* Get phpcs binary
*
Expand All @@ -83,15 +89,23 @@ public static function getPhpCsBinary(): string
{
$path = __DIR__ . '/../vendor/bin/phpcs';

if (self::isWindows()) {
if (self::isWindows() === true) {
$path = str_replace('\\', '/', __DIR__ . '/../vendor/bin/phpcs.bat');
}

return $path;
}


/**
* Checks the OS and returns true if Windows system is found.
*
* @return boolean
*/
public static function isWindows(): bool
{
return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
}


}

0 comments on commit 8bf8108

Please sign in to comment.