Skip to content

Commit

Permalink
Fix the PHPCSFixer return result
Browse files Browse the repository at this point in the history
  • Loading branch information
kayw-geek committed Oct 14, 2022
1 parent d8d74fa commit ea9da30
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion quality
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/../../autoload.php';
require __DIR__ . '/vendor/autoload.php';

use Kayw\QualityHook\Command\RunCommand;
use Symfony\Component\Console\Application;
Expand Down
25 changes: 15 additions & 10 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,24 @@ private function checkPhpCsFixerConfigFileExist(): bool
return false;
}

private function executeRun(array $active_options, InputInterface $input, OutputInterface $output): int
private static function getChangedFilesString():?string
{
exec('git status -v', $exec_output);

if ($exec_output === '') {
return self::SUCCESS;
return null;
}

$change_files_string = implode(' ', array_map(function (string $output_line): string {
return implode(' ', array_map(function (string $output_line): string {
return trim(substr($output_line, 2));
}, array_filter($exec_output, fn (string $output_line) => !str_starts_with($output_line, 'D'))));
}, array_filter($exec_output, fn (string $output_line) => !str_starts_with($output_line, ' M'))));

}
private function executeRun(array $active_options, InputInterface $input, OutputInterface $output): int
{
$changed_files_string = self::getChangedFilesString();

if ($change_files_string === '') {
if ($changed_files_string === null) {
return self::SUCCESS;
}

Expand All @@ -97,8 +102,8 @@ private function executeRun(array $active_options, InputInterface $input, Output

foreach ($active_options as $active_option) {
$result_code += match ($active_option) {
self::OPTION_PHP_FIXER => $this->runPhpCsFixerFix($output, $change_files_string),
self::OPTION_PHPSTAN => $this->runPhpStanFix($output, $change_files_string),
self::OPTION_PHP_FIXER => $this->runPhpCsFixerFix($output, $changed_files_string),
self::OPTION_PHPSTAN => $this->runPhpStanFix($output, $changed_files_string),
};
}

Expand Down Expand Up @@ -134,11 +139,11 @@ private function runPhpCsFixerFix(OutputInterface $output, string $change_files_
$output->writeln('<info> PHP Cs Fixer Fixing ... </info>');
$base_path = $this->getPath();
exec("$base_path/vendor/bin/php-cs-fixer fix --config $base_path/$this->phpCsFixerConfig $change_files_string", $execute_output, $result_code);

if (!$execute_output == []) {
if ($execute_output !== [] && $execute_output[0] !== '') {
$output->writeln('<error> Some Files have fixed, please re-commit these changes</error>');
return self::FAILURE;
}

return $result_code;
}

Expand Down

0 comments on commit ea9da30

Please sign in to comment.