Skip to content

Commit

Permalink
Merge pull request #5 from Clearfacts/pre-commit-psalm
Browse files Browse the repository at this point in the history
[pre-commit-psalm] pre-commit psalm hook, autofix phpcs
  • Loading branch information
ctrl-f5 committed Mar 9, 2023
2 parents aab5be9 + f1cd257 commit 67a722b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
9 changes: 0 additions & 9 deletions src/Command/CopyCsConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function copyConfig(string $destination, string $type, string $template): void
{
$modified = @filemtime($destination);
if ($modified && (time() - $modified < 604800)) {
if (!$this->quiet) {
$this->io?->warning("$type config already exists and is less than a week old");
}

return;
}

$files = $this->getFinder()
->ignoreDotFiles(false)
->in(__DIR__ . '/../../templates/cs')
Expand Down
6 changes: 5 additions & 1 deletion templates/cs/.php-cs-fixer-8.1.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
'concat_space' => ['spacing' => 'one'],
'php_unit_method_casing' => ['case' => 'camel_case'],
'phpdoc_to_comment' => false,
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'parameters']],
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'parameters', 'match']],
'no_unused_imports' => true,
'heredoc_indentation' => true,
'ternary_to_null_coalescing' => true,
'assign_null_coalescing_to_coalesce_equal' => true,
'single_line_throw' => false,
])
->setFinder($finder)
;
2 changes: 1 addition & 1 deletion templates/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ scriptPath="$(dirname -- "$thisScript")"

make -s copy-cs-config options="--quiet"

hooks="pre-commit-phpcs pre-commit-eslint pre-commit-twig %custom_hooks%"
hooks="pre-commit-phpcs pre-commit-psalm pre-commit-eslint pre-commit-twig %custom_hooks%"

for hook in $hooks
do
Expand Down
10 changes: 9 additions & 1 deletion templates/hooks/pre-commit-phpcs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ then
exit 0
fi

if [ "${CF_COMMIT_PHPCS_AUTOFIX_ENABLED:-0}" == 1 ]
then
# Run fixer and add changes to the commit
make -s phpcs-fix files="${files}"
git add ${files}
exit 0
fi

# Construct the phpcs command.
command=$(echo "make -s phpcs files=\"${files}\"")
echo "$command"
Expand Down Expand Up @@ -36,4 +44,4 @@ then
# Exit so you can stage the files and commit again
returnCode=1
fi
exit $returnCode
exit $returnCode
18 changes: 18 additions & 0 deletions templates/hooks/pre-commit-psalm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

if [ "${CF_COMMIT_PSALM_ENABLED:-1}" != 1 ]
then
echo "pre-commit psalm disabled"
exit 0
fi

# Grab the staged files
stagedFiles=($(git diff --cached --name-only --diff-filter=AM | grep -e .php$))
files="${stagedFiles[@]}"
if [ "$files" = '' ]
then
echo "psalm: no php files to check"
exit 0
fi

make -s det cmd="vendor/bin/psalm --no-progress --no-cache ${files}"

0 comments on commit 67a722b

Please sign in to comment.