Skip to content

Commit

Permalink
OXDEV-8655 Integrate EasyCodingStandards with hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
liulka-oxid committed Dec 6, 2024
1 parent e276572 commit 52142b8
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
41 changes: 41 additions & 0 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

# get bash colors and styles here:
# http://misc.flogisoft.com/bash/tip_colors_and_formatting
style_reset='\e[0m'
style_red='\e[31m'
style_green='\e[32m'
style_bold='\e[1m'
style_underline='\e[4m'
style_dim='\e[2m'

function __run() #(step, name, cmd)
{
local output exitcode

printf "[%s] %-20s" "$1" "$2"
output=$(eval "$3" 2>&1)
exitcode=$?

if [[ 0 == "$exitcode" || 130 == "$exitcode" ]]; then
echo -e " ${style_green}OK!${style_reset}"
else
echo -e " ${style_red}ERROR!${style_reset}"
echo -e "\n${style_bold}Git commit was interrupted!${style_reset}"
echo -e "${style_underline}$2${style_reset} run failed:"
echo -e "${style_dim}$3${style_reset}${style_bold}$output${style_reset}\n"
echo -e "Fix all the reported issues and try again. Or run commit command with \"--no-verify\" option to skip all configured pre-commit hooks."
exit 1
fi
}

php="docker-compose exec -T php php"
internalDir="source/Internal"
testsDir="tests/"

modified="git diff --cached --name-only --diff-filter=ACM | grep '.php$'"
ecs="${php} vendor/bin/ecs check ${internalDir} ${testsDir} --no-diffs --no-progress-bar"

echo -e "Running pre-commit hooks:"
__run "1/2" "PHP lint" "${modified} | xargs -r ${php} -l"
__run "2/2" "Easy Coding Standard" "${ecs}"
12 changes: 12 additions & 0 deletions .git-hooks/symlink.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
if [ ! -L .git/hooks ];
then
echo ".git/hooks is not symlink"
echo "copying .git/hooks to .git/old_hooks"
mv .git/hooks .git/old_hooks

echo "symlinking ../.git-hooks .git/hooks"
ln -s ../.git-hooks .git/hooks
else
echo ".git/hooks is already a symlink"
fi
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"phpunit/phpunit": "^11.4",
"squizlabs/php_codesniffer": "^3.5.4",
"symfony/browser-kit": "^6.4",
"symfony/http-client": "^6.4"
"symfony/http-client": "^6.4",
"symplify/easy-coding-standard": "^12.3"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down Expand Up @@ -94,10 +95,12 @@
},
"scripts": {
"post-install-cmd": [
"@oe:ide-helper:generate"
"@oe:ide-helper:generate",
"[ $COMPOSER_DEV_MODE -eq 0 ] || ./.git-hooks/symlink.sh"
],
"post-update-cmd": [
"@oe:ide-helper:generate"
"@oe:ide-helper:generate",
"[ $COMPOSER_DEV_MODE -eq 0 ] || ./.git-hooks/symlink.sh"
],
"oe:ide-helper:generate": [
"if [ -f ./vendor/bin/oe-eshop-ide_helper ]; then oe-eshop-ide_helper; fi"
Expand Down
63 changes: 63 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

use PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer;
use PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withRules([
BlankLineBetweenImportGroupsFixer::class,
])
->withPreparedSets(
psr12: true,
common: true,
strict: true,
)
->withConfiguredRule(
HeaderCommentFixer::class,
[
'header' => 'Copyright © OXID eSales AG. All rights reserved.
See LICENSE file for license details.',
'location' => 'after_open',
'comment_type' => 'PHPDoc'
]
)
->withConfiguredRule(
ClassAttributesSeparationFixer::class,
[
'elements' => [
'property' => ClassAttributesSeparationFixer::SPACING_NONE,
'const' => ClassAttributesSeparationFixer::SPACING_NONE,
],
]
)
->withConfiguredRule(
OrderedImportsFixer::class,
[
'sort_algorithm' => OrderedImportsFixer::SORT_ALPHA,
'imports_order' => [
OrderedImportsFixer::IMPORT_TYPE_CLASS,
OrderedImportsFixer::IMPORT_TYPE_FUNCTION,
OrderedImportsFixer::IMPORT_TYPE_CONST,
]
]
)
->withSkip(
[
NotOperatorWithSuccessorSpaceFixer::class,
DeclareStrictTypesFixer::class => [
'*Interface.php',
]
],
);

0 comments on commit 52142b8

Please sign in to comment.