Skip to content

Commit

Permalink
Using PHPCSFixer and improve get changed file list
Browse files Browse the repository at this point in the history
  • Loading branch information
kayw-geek committed Aug 1, 2023
1 parent 4c260e6 commit e8b5a03
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea/
.DS_Store
/vendor/
.php-cs-fixer.cache
composer.lock
Expand Down
73 changes: 73 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->ignoreDotFiles(false)
->ignoreVCSIgnored(true)
->exclude(['dev-tools/phpstan', 'tests/Fixtures'])
->in(__DIR__);

$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
'@PSR12' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP80Migration' => true,
'@PHP80Migration:risky' => true,
'align_multiline_comment' => true,
'array_indentation' => true,
'binary_operator_spaces' => ['operators' => ['=' => 'align_single_space_minimal', '=>' => 'align_single_space_minimal', '??=' => 'align_single_space_minimal', '|' => 'no_space']],
'blank_line_before_statement' => ['statements' => ['do', 'for', 'foreach', 'if', 'return', 'switch', 'while']],
'class_attributes_separation' => ['elements' => ['method' => 'one', 'trait_import' => 'none']],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'concat_space' => ['spacing' => 'one'],
'echo_tag_syntax' => ['format' => 'long'],
'escape_implicit_backslashes' => true,
'fopen_flags' => false,
'fully_qualified_strict_types' => false,
'function_to_constant' => ['functions' => ['get_class', 'get_called_class', 'php_sapi_name', 'phpversion', 'pi']],
'heredoc_to_nowdoc' => true,
'increment_style' => ['style' => 'post'],
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline', 'keep_multiple_spaces_after_comma' => false, 'after_heredoc' => true],
'method_chaining_indentation' => true,
'modernize_types_casting' => false,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => ['strategy' => 'no_multi_line'],
'native_constant_invocation' => false,
'native_function_invocation' => false,
'no_alternative_syntax' => true,
'no_null_property_initialization' => true,
'no_superfluous_phpdoc_tags' => false,
'no_unneeded_curly_braces' => true,
'no_useless_return' => true,
'not_operator_with_space' => false,
'nullable_type_declaration_for_default_null_value' => true,
'operator_linebreak' => true,
'ordered_class_elements' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
'php_unit_strict' => true,
'php_unit_test_case_static_method_calls' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_annotation_without_dot' => false,
'phpdoc_no_alias_tag' => false,
'phpdoc_no_empty_return' => false,
'phpdoc_summary' => false, // no need to add dot at the end of short description
'phpdoc_to_comment' => false, // allow use of docblock comment in function body
'phpdoc_var_annotation_correct_order' => true,
'pow_to_exponentiation' => false,
'self_static_accessor' => true,
'simplified_null_return' => false,
'strict_comparison' => true,
'strict_param' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'parameters', 'match']],
'use_arrow_functions' => false,
'whitespace_after_comma_in_array' => true,
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false, 'always_move_variable' => false],
])
->setFinder($finder);

return $config;
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"symfony/console": "^6.0.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer":"*",
"friendsofphp/php-cs-fixer":"^3.22",
"phpstan/phpstan":"*"
},
"bin": ["quality"]
Expand Down
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 file_exists(__DIR__ . '/vendor/autoload.php') ? __DIR__ . '/vendor/autoload.php' : __DIR__ . '/../../autoload.php';

use Kayw\QualityHook\Command\RunCommand;
use Symfony\Component\Console\Application;
Expand Down
30 changes: 17 additions & 13 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return match ($input->getArguments() !== []) {
$input->getArgument(self::ARGUMENT_INSTALL) !== null => $this->executeInstall($active_options, $input, $output),
$input->getArgument(self::ARGUMENT_UNINSTALL) !== null => $this->executeUninstall($input, $output),
default => $this->executeRun($active_options, $input, $output)
default => $this->executeRun($active_options, $input, $output),
};
}

private function checkBinInstall(string $bin_name): bool
{
return file_exists($this->getPath() .'/vendor/bin/'. $bin_name);
return file_exists($this->getPath() . '/vendor/bin/' . $bin_name);
}

private function checkPhpCsFixerConfigFileExist(): bool
Expand All @@ -67,22 +67,20 @@ private function checkPhpCsFixerConfigFileExist(): bool
return false;
}

private static function getChangedFilesString():?string
private static function getChangedFilesString(): ?string
{
exec('git status -v', $exec_output);
exec("git status -s | grep '^ M' | grep -v '^ D' | awk '{print $2}'", $exec_output);

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

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, ' M'))));

return implode(' ', $exec_output);
}

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

if ($changed_files_string === null) {
return self::SUCCESS;
Expand Down Expand Up @@ -128,29 +126,35 @@ private function getPath(): string
private function runPhpCsFixerFix(OutputInterface $output, string $change_files_string): int
{
if (!$this->checkBinInstall(self::PHPCSFIXER_BIN_NAME)) {
$output->writeln("<error> The PHPCsFixer packages not install in your project</error>");
$output->writeln('<error> The PHPCsFixer packages not install in your project</error>');

return self::FAILURE;
}

if (!$this->checkPhpCsFixerConfigFileExist()) {
$output->writeln('<error> The PHPCsFixer config file not found on your project </error>');

return self::FAILURE;
}

$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 !== [] && $execute_output[0] !== '') {
$output->writeln('<error> Some Files have fixed, please re-commit these changes</error>');

return self::FAILURE;
}

return $result_code;
}

private function runPhpStanFix(OutputInterface $output, string $change_files_string): int
{
if (!$this->checkBinInstall(self::PHPSTAN_BIN_NAME)) {
$output->writeln("<error> The PHPStan packages not install in your project</error>");
$output->writeln('<error> The PHPStan packages not install in your project</error>');

return self::FAILURE;
}

Expand All @@ -173,7 +177,7 @@ private function runPhpStanFix(OutputInterface $output, string $change_files_str

private function executeInstall(array $active_options, InputInterface $input, OutputInterface $output): int
{
$option_text = implode(' ', array_map(fn (string $active_option): string => '--' . $active_option, $active_options));
$option_text = implode(' ', array_map(fn (string $active_option): string => '--' . $active_option, $active_options));
$execute_command = <<<EOF
#!/bin/bash
exec </dev/tty
Expand Down

0 comments on commit e8b5a03

Please # to comment.