From 1c060c8d00dc1987ea4ea78995ed028729d0ab69 Mon Sep 17 00:00:00 2001 From: Martin Supiot Date: Thu, 13 Feb 2025 15:50:09 +0100 Subject: [PATCH 1/7] fix: lint and phpcs dockerfile --- .editorconfig | 2 +- Taskfile.yml | 42 ++++--- alma-gateway-for-woocommerce.php | 2 + bin/coding-standard-fixer.sh | 11 +- bin/coding-standard.sh | 11 +- compose.yml | 2 +- dockerfile.md | 25 ++++ includes/AlmaSettings.php | 160 +++++++++++++------------ includes/Blocks/AlmaBlock.php | 5 - includes/Blocks/AlmaWidgetBlock.php | 7 +- includes/Handlers/GenericHandler.php | 2 +- Dockerfile.legacy => legacy.Dockerfile | 0 phpcbf.Dockerfile | 54 +++++++++ phpcs.Dockerfile | 53 ++++++++ sonar-project.properties | 2 +- 15 files changed, 264 insertions(+), 114 deletions(-) create mode 100644 dockerfile.md rename Dockerfile.legacy => legacy.Dockerfile (100%) create mode 100644 phpcbf.Dockerfile create mode 100644 phpcs.Dockerfile diff --git a/.editorconfig b/.editorconfig index e755404c..017936ab 100644 --- a/.editorconfig +++ b/.editorconfig @@ -776,7 +776,7 @@ ij_json_wrap_long_lines = false [{*.htm,*.html,*.sht,*.shtm,*.shtml}] ij_html_add_new_line_before_tags = body,div,p,form,h1,h2,h3 -ij_html_align_attributes = true +ij_html_align_attributes = false ij_html_align_text = false ij_html_attribute_wrap = normal ij_html_block_comment_add_space = false diff --git a/Taskfile.yml b/Taskfile.yml index a738dab8..85091ace 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -18,6 +18,34 @@ tasks: cmds: - docker compose build --build-arg UID="$(id -u)" php5-6 + build:phpcs: + desc: Build PHPCS container + cmds: + - docker build -t phpcs -f phpcs.Dockerfile . + + build:phpcbf: + desc: Build PHPCBF container + cmds: + - docker build -t phpcbf -f phpcbf.Dockerfile . + + lint: + aliases: + - run:phpcs + desc: Run PHPCS container + deps: + - build:phpcs + cmds: + - ./bin/coding-standard.sh + + lint:fix: + aliases: + - run:phpcbf + desc: Run PHPCBF container + deps: + - build:phpcbf + cmds: + - ./bin/coding-standard-fixer.sh + test: desc: Run tests deps: @@ -32,20 +60,6 @@ tasks: - pre-commit install --hook-type commit-msg - pre-commit install - lint: - desc: Run linter - deps: - - build:php5-6 - cmds: - - docker compose run --rm php5-6 php ./vendor/bin/phpcs --standard=phpcs.xml ./ - - lint:fix: - desc: Run linter with autofix - deps: - - build:php5-6 - cmds: - - docker compose run --rm php5-6 php ./vendor/bin/phpcbf --standard=phpcs.xml ./ - php-compatibility: desc: Check compatibility code deps: diff --git a/alma-gateway-for-woocommerce.php b/alma-gateway-for-woocommerce.php index d678f331..2f7a864e 100644 --- a/alma-gateway-for-woocommerce.php +++ b/alma-gateway-for-woocommerce.php @@ -67,6 +67,8 @@ * @return Alma\Woocommerce\AlmaPlugin * @noinspection PhpIncludeInspection */ + + function alma_plugin() { static $plugin; diff --git a/bin/coding-standard-fixer.sh b/bin/coding-standard-fixer.sh index d244b8a4..dfb88518 100755 --- a/bin/coding-standard-fixer.sh +++ b/bin/coding-standard-fixer.sh @@ -1,7 +1,8 @@ #!/bin/bash -vendor/bin/phpcbf --standard=phpcs.xml ./ -if [ $? != 0 ] -then - echo "Fix the errors with PHPcbf automatic fixer before commit!" - exit 1 +PROJECT_ROOT=$(git rev-parse --show-toplevel) +docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcbf --standard=phpcs.xml ./ +EXIT_CODE=$? +if [[ $EXIT_CODE -ne 0 ]]; then + echo "Fix the errors with PHPcbf automatic fixer before commit!" + exit 1 fi diff --git a/bin/coding-standard.sh b/bin/coding-standard.sh index 39a33f3c..9032a21b 100755 --- a/bin/coding-standard.sh +++ b/bin/coding-standard.sh @@ -1,7 +1,8 @@ #!/bin/bash -vendor/bin/phpcs --standard=phpcs.xml ./ -if [ $? != 0 ] -then - echo "Fix the errors before commit!" - exit 1 +PROJECT_ROOT=$(git rev-parse --show-toplevel) +docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcs --standard=phpcs.xml ./ +EXIT_CODE=$? +if [[ $EXIT_CODE -ne 0 ]]; then + echo "Fix the errors before commit!" + exit 1 fi diff --git a/compose.yml b/compose.yml index 160bcc23..79bd7ab4 100644 --- a/compose.yml +++ b/compose.yml @@ -40,7 +40,7 @@ services: php5-6: build: context: ./ - dockerfile: Dockerfile.legacy + dockerfile: legacy.Dockerfile volumes: - ./:/home/phpuser - /home/phpuser/src/vendor # do not mount vendor inside container diff --git a/dockerfile.md b/dockerfile.md new file mode 100644 index 00000000..53979ec5 --- /dev/null +++ b/dockerfile.md @@ -0,0 +1,25 @@ +# Dockerfiles + +## Dockerfile + +Used to run the tests with the command + +```task test``` + +*Tests need MySQL to run. The MySQL container is started by the `docker-compose.yml` file.* + +## Legacy Dockerfile + +Used to build the ZIP file to release the plugin. + +## PHPCS Dockerfile + +Used to run the `phpcs` command by the pre-commit hook or manually with the command + +```task lint``` + +## PHPCBF Dockerfile + +Used to run the `phpcbf` command by the pre-commit hook or manually with the command + +```task lint:fix``` \ No newline at end of file diff --git a/includes/AlmaSettings.php b/includes/AlmaSettings.php index cba71050..7b9bfd31 100644 --- a/includes/AlmaSettings.php +++ b/includes/AlmaSettings.php @@ -187,6 +187,10 @@ class AlmaSettings { * @var PluginFactory */ protected $plugin_factory; + /** + * @var mixed + */ + private $alma_settings; /** * Constructor. @@ -421,15 +425,6 @@ public function has_pay_now() { return false; } - /** - * Is plugin enabled. - * - * @return bool - */ - public function is_enabled() { - return 'yes' === $this->enabled; - } - /** * Gets title for a payment method. * @@ -502,55 +497,6 @@ public function get_environment() { return 'live' === $this->environment ? 'live' : 'test'; } - /** - * Check if we have keys for the active environment. - * - * @return bool - */ - public function has_keys() { - if ( empty( $this->get_active_api_key() ) ) { - return false; - } - - return true; - } - - /** - * Gets API string for the current environment. - * - * @return string - */ - public function get_active_api_key() { - return $this->is_live() ? $this->get_live_api_key() : $this->get_test_api_key(); - } - - /** - * Is using live API. - * - * @return bool - */ - public function is_live() { - return $this->get_environment() === 'live'; - } - - /** - * Gets API key for live environment. - * - * @return string - */ - public function get_live_api_key() { - return $this->encryptor_helper->decrypt( $this->live_api_key ); - } - - /** - * Gets API key for test environment. - * - * @return string - */ - public function get_test_api_key() { - return $this->encryptor_helper->decrypt( $this->test_api_key ); - } - /** * Fetch the payment. * @@ -604,6 +550,42 @@ public function get_alma_client() { ); } + /** + * Gets API string for the current environment. + * + * @return string + */ + public function get_active_api_key() { + return $this->is_live() ? $this->get_live_api_key() : $this->get_test_api_key(); + } + + /** + * Is using live API. + * + * @return bool + */ + public function is_live() { + return $this->get_environment() === 'live'; + } + + /** + * Gets API key for live environment. + * + * @return string + */ + public function get_live_api_key() { + return $this->encryptor_helper->decrypt( $this->live_api_key ); + } + + /** + * Gets API key for test environment. + * + * @return string + */ + public function get_test_api_key() { + return $this->encryptor_helper->decrypt( $this->test_api_key ); + } + /** * Create the payment. * @@ -903,13 +885,12 @@ function ( $fee_plan ) { if ( ! isset( $this->settings[ $enabled_key ] ) ) { $this->settings[ $enabled_key ] = ConstantsHelper::DEFAULT_FEE_PLAN === $plan_key ? 'yes' : 'no'; } - $this->settings[ "deferred_months_$plan_key" ] = $fee_plan->getDeferredMonths(); - $this->settings[ "deferred_days_$plan_key" ] = $fee_plan->getDeferredDays(); - $this->settings[ "installments_count_$plan_key" ] = $fee_plan->getInstallmentsCount(); + $this->settings[ sprintf( 'deferred_months_%s', $plan_key ) ] = $fee_plan->getDeferredMonths(); + $this->settings[ sprintf( 'deferred_days_%s', $plan_key ) ] = $fee_plan->getDeferredDays(); + $this->settings[ sprintf( 'installments_count_%s', $plan_key ) ] = $fee_plan->getInstallmentsCount(); } } - /** * Retrieves the api fee plans. * @@ -973,7 +954,6 @@ public function has_pnx_4() { return false; } - /** * Tells if the merchant has at least one "pnx_plus_4" payment method enabled in the WC back-office. * @@ -1028,11 +1008,16 @@ protected function is_eligible( $plan, $amount ) { * @return bool */ public function should_display_plan( $plan_key, $gateway_id ) { + + $installment_count = $this->get_installments_count( $plan_key ); + $deferred_days = $this->get_deferred_days( $plan_key ); + $deferred_months = $this->get_deferred_months( $plan_key ); + switch ( $gateway_id ) { case ConstantsHelper::GATEWAY_ID: case ConstantsHelper::GATEWAY_ID_IN_PAGE: $display_plan = in_array( - $this->get_installments_count( $plan_key ), + $installment_count, array( 2, 3, @@ -1043,17 +1028,15 @@ public function should_display_plan( $plan_key, $gateway_id ) { break; case ConstantsHelper::GATEWAY_ID_PAY_NOW: case ConstantsHelper::GATEWAY_ID_IN_PAGE_PAY_NOW: - $display_plan = $this->get_installments_count( $plan_key ) === 1 - && ( $this->get_deferred_days( $plan_key ) === 0 && $this->get_deferred_months( $plan_key ) === 0 ); + $display_plan = 1 === $installment_count && ( 0 === $deferred_days && 0 === $deferred_months ); break; case ConstantsHelper::GATEWAY_ID_PAY_LATER: case ConstantsHelper::GATEWAY_ID_IN_PAGE_PAY_LATER: - $display_plan = $this->get_installments_count( $plan_key ) === 1 - && ( $this->get_deferred_days( $plan_key ) !== 0 || $this->get_deferred_months( $plan_key ) !== 0 ); + $display_plan = 1 === $installment_count && ( 0 !== $deferred_days || 0 !== $deferred_months ); break; case ConstantsHelper::GATEWAY_ID_IN_PAGE_MORE_THAN_FOUR: case ConstantsHelper::GATEWAY_ID_MORE_THAN_FOUR: - $display_plan = $this->get_installments_count( $plan_key ) > 4; + $display_plan = $installment_count > 4; break; default: $display_plan = false; @@ -1098,28 +1081,27 @@ public function get_display_text() { */ public function build_fee_plan( $plan_key ) { - if ( ! isset( $this->settings[ "installments_count_$plan_key" ] ) ) { + if ( ! isset( $this->settings[ sprintf( 'installments_count_%s', $plan_key ) ] ) ) { throw new PlansDefinitionException( "installments_count_$plan_key not set" ); } - if ( ! isset( $this->settings[ "deferred_days_$plan_key" ] ) ) { + if ( ! isset( $this->settings[ sprintf( 'deferred_days_%s', $plan_key ) ] ) ) { throw new PlansDefinitionException( "deferred_days_$plan_key not set" ); } - if ( ! isset( $this->settings[ "deferred_months_$plan_key" ] ) ) { + if ( ! isset( $this->settings[ sprintf( 'deferred_months_%s', $plan_key ) ] ) ) { throw new PlansDefinitionException( "deferred_months_$plan_key not set" ); } return new FeePlan( array( - 'installments_count' => $this->settings[ "installments_count_$plan_key" ], - 'deferred_days' => $this->settings[ "deferred_days_$plan_key" ], - 'deferred_months' => $this->settings[ "deferred_months_$plan_key" ], + 'installments_count' => $this->settings[ sprintf( 'installments_count_%s', $plan_key ) ], + 'deferred_days' => $this->settings[ sprintf( 'deferred_days_%s', $plan_key ) ], + 'deferred_months' => $this->settings[ sprintf( 'deferred_months_%s', $plan_key ) ], ) ); } - /** * Does need API key ? * @@ -1167,4 +1149,30 @@ public function get_variable_product_sale_price_query_selector() { return $this->settings_helper->default_variable_sale_price_selector(); } + + public function is_widget_can_be_displayed() { + return $this->alma_settings->has_keys() && $this->alma_settings->is_enabled() && 'yes' === $this->alma_settings->display_cart_eligibility; + } + + /** + * Check if we have keys for the active environment. + * + * @return bool + */ + public function has_keys() { + if ( empty( $this->get_active_api_key() ) ) { + return false; + } + + return true; + } + + /** + * Is plugin enabled. + * + * @return bool + */ + public function is_enabled() { + return 'yes' === $this->enabled; + } } diff --git a/includes/Blocks/AlmaBlock.php b/includes/Blocks/AlmaBlock.php index 937b6adf..6238b170 100644 --- a/includes/Blocks/AlmaBlock.php +++ b/includes/Blocks/AlmaBlock.php @@ -13,9 +13,7 @@ use Alma\Woocommerce\AlmaLogger; use Alma\Woocommerce\AlmaSettings; -use Alma\Woocommerce\Builders\Helpers\CartHelperBuilder; use Alma\Woocommerce\Builders\Helpers\GatewayHelperBuilder; -use Alma\Woocommerce\Builders\Helpers\PlanHelperBuilder; use Alma\Woocommerce\Builders\Helpers\ToolsHelperBuilder; use Alma\Woocommerce\Exceptions\AlmaException; use Alma\Woocommerce\Gateways\Standard\StandardGateway; @@ -24,7 +22,6 @@ use Alma\Woocommerce\Helpers\CheckoutHelper; use Alma\Woocommerce\Helpers\ConstantsHelper; use Alma\Woocommerce\Helpers\GatewayHelper; -use Alma\Woocommerce\Helpers\PlanHelper; use Alma\Woocommerce\Helpers\ToolsHelper; use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; @@ -105,8 +102,6 @@ public function initialize() { * @return mixed */ public function is_active() { - // TODO need to check if no side effects - // return $this->gateway->is_available(); return true; } diff --git a/includes/Blocks/AlmaWidgetBlock.php b/includes/Blocks/AlmaWidgetBlock.php index 4ee463ac..bc7fc09d 100644 --- a/includes/Blocks/AlmaWidgetBlock.php +++ b/includes/Blocks/AlmaWidgetBlock.php @@ -15,8 +15,8 @@ use Alma\Woocommerce\Builders\Helpers\CartHelperBuilder; use Alma\Woocommerce\Builders\Helpers\GatewayHelperBuilder; use Alma\Woocommerce\Helpers\CartHelper; -use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface; use Alma\Woocommerce\Helpers\ConstantsHelper; +use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface; if ( ! defined( 'ABSPATH' ) ) { die( 'Not allowed' ); // Exit if accessed directly. @@ -202,9 +202,6 @@ function ( $plan_definition ) { } private function can_be_displayed() { - return $this->alma_settings->has_keys() - && $this->alma_settings->is_enabled() - && 'yes' === $this->alma_settings->display_cart_eligibility - && ! $this->gateway_helper->cart_contains_excluded_category(); + return $this->alma_settings->is_widget_can_be_displayed() && ! $this->gateway_helper->cart_contains_excluded_category(); } } diff --git a/includes/Handlers/GenericHandler.php b/includes/Handlers/GenericHandler.php index f04f2959..5a061c03 100644 --- a/includes/Handlers/GenericHandler.php +++ b/includes/Handlers/GenericHandler.php @@ -175,7 +175,7 @@ protected function inject_payment_plan_widget( $exclusion_msg = $this->get_cart_not_eligible_message_gift_cards(); ?> Alma + style="width: auto !important; height: 25px !important; border: none !important; vertical-align: middle; display: inline-block;"> Date: Fri, 14 Feb 2025 09:23:48 +0100 Subject: [PATCH 2/7] fix: change phpcs configuration --- Taskfile.yml | 4 ++-- bin/coding-standard-fixer.sh | 2 +- bin/coding-standard.sh | 2 +- phpcs.xml | 3 +-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index 85091ace..00ad86a6 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -21,12 +21,12 @@ tasks: build:phpcs: desc: Build PHPCS container cmds: - - docker build -t phpcs -f phpcs.Dockerfile . + - docker build -t phpcs:wp -f phpcs.Dockerfile . build:phpcbf: desc: Build PHPCBF container cmds: - - docker build -t phpcbf -f phpcbf.Dockerfile . + - docker build -t phpcbf:wp -f phpcbf.Dockerfile . lint: aliases: diff --git a/bin/coding-standard-fixer.sh b/bin/coding-standard-fixer.sh index dfb88518..9da72900 100755 --- a/bin/coding-standard-fixer.sh +++ b/bin/coding-standard-fixer.sh @@ -1,6 +1,6 @@ #!/bin/bash PROJECT_ROOT=$(git rev-parse --show-toplevel) -docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcbf --standard=phpcs.xml ./ +docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcbf:wp --standard=phpcs.xml ./ EXIT_CODE=$? if [[ $EXIT_CODE -ne 0 ]]; then echo "Fix the errors with PHPcbf automatic fixer before commit!" diff --git a/bin/coding-standard.sh b/bin/coding-standard.sh index 9032a21b..d5b47154 100755 --- a/bin/coding-standard.sh +++ b/bin/coding-standard.sh @@ -1,6 +1,6 @@ #!/bin/bash PROJECT_ROOT=$(git rev-parse --show-toplevel) -docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcs --standard=phpcs.xml ./ +docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcs:wp --standard=phpcs.xml ./ EXIT_CODE=$? if [[ $EXIT_CODE -ne 0 ]]; then echo "Fix the errors before commit!" diff --git a/phpcs.xml b/phpcs.xml index c2df2624..b748e7d5 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,7 +1,6 @@ - + xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd"> A custom set of rules to check for Alma Woocommerce Gateway From 888c6e61aa5024526c41926e063851a9d3ca7646 Mon Sep 17 00:00:00 2001 From: Martin Supiot Date: Fri, 21 Feb 2025 18:06:29 +0100 Subject: [PATCH 3/7] fix: refactor linting to be like prestashop --- .pre-commit-config.yaml | 4 +- Taskfile.yml | 46 ++++++++-------- bin/{coding-standard-fixer.sh => lint-fix.sh} | 3 +- bin/{coding-standard.sh => lint.sh} | 3 +- bin/php-compatibility.sh | 9 ++++ composer.json | 6 +++ dockerfile.md | 7 +-- lint.Dockerfile | 25 +++++++++ phpcbf.Dockerfile | 54 ------------------- phpcs.Dockerfile | 53 ------------------ 10 files changed, 68 insertions(+), 142 deletions(-) rename bin/{coding-standard-fixer.sh => lint-fix.sh} (68%) rename bin/{coding-standard.sh => lint.sh} (65%) create mode 100755 bin/php-compatibility.sh create mode 100644 lint.Dockerfile delete mode 100644 phpcbf.Dockerfile delete mode 100644 phpcs.Dockerfile diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 529032c5..c708b367 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,13 +23,13 @@ repos: - id: coding-standard-fixer name: PHP and WordPress coding standards Fixer test language: system - entry: ./bin/coding-standard-fixer.sh + entry: ./bin/lint-fix.sh stages: [ pre-commit ] - id: coding-standard name: Check the PHP and WordPress coding standards language: system - entry: ./bin/coding-standard.sh + entry: ./bin/lint.sh stages: [ pre-commit ] # - repo: https://github.com/returntocorp/semgrep diff --git a/Taskfile.yml b/Taskfile.yml index 00ad86a6..88c78ecd 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -18,33 +18,36 @@ tasks: cmds: - docker compose build --build-arg UID="$(id -u)" php5-6 - build:phpcs: - desc: Build PHPCS container + lint:build: + desc: Build lint container (php-cs-fixer) cmds: - - docker build -t phpcs:wp -f phpcs.Dockerfile . - - build:phpcbf: - desc: Build PHPCBF container - cmds: - - docker build -t phpcbf:wp -f phpcbf.Dockerfile . + - docker build -t lint:wc -f lint.Dockerfile . lint: - aliases: - - run:phpcs - desc: Run PHPCS container + desc: Run lint container deps: - - build:phpcs + - lint:build cmds: - - ./bin/coding-standard.sh + - ./bin/lint.sh lint:fix: - aliases: - - run:phpcbf - desc: Run PHPCBF container + desc: Run lint container deps: - - build:phpcbf + - lint:build + cmds: + - ./bin/lint-fix.sh + + php-compatibility:build: + desc: Build php-compatibility container (phpcs) cmds: - - ./bin/coding-standard-fixer.sh + - docker build -t php-compatibility:wc -f lint.Dockerfile . + + php-compatibility: + desc: Check compatibility code in PHP-CS container + deps: + - php-compatibility:build + cmds: + - ./bin/php-compatibility.sh test: desc: Run tests @@ -60,13 +63,6 @@ tasks: - pre-commit install --hook-type commit-msg - pre-commit install - php-compatibility: - desc: Check compatibility code - deps: - - build:php5-6 - cmds: - - docker compose run --rm php5-6 php ./vendor/bin/phpcs -d memory_limit=512M -p ./ --standard=PHPCompatibility -s --runtime-set testVersion 5.6-8.1 --ignore='*/vendor/*,build/*' - dist: desc: Build zip file deps: diff --git a/bin/coding-standard-fixer.sh b/bin/lint-fix.sh similarity index 68% rename from bin/coding-standard-fixer.sh rename to bin/lint-fix.sh index 9da72900..d12c80b1 100755 --- a/bin/coding-standard-fixer.sh +++ b/bin/lint-fix.sh @@ -1,7 +1,8 @@ #!/bin/bash PROJECT_ROOT=$(git rev-parse --show-toplevel) -docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcbf:wp --standard=phpcs.xml ./ +docker run --rm -v "$PROJECT_ROOT:/app" -w /app lint:wc -v --standard=phpcs.xml ./ EXIT_CODE=$? + if [[ $EXIT_CODE -ne 0 ]]; then echo "Fix the errors with PHPcbf automatic fixer before commit!" exit 1 diff --git a/bin/coding-standard.sh b/bin/lint.sh similarity index 65% rename from bin/coding-standard.sh rename to bin/lint.sh index d5b47154..ef9c5256 100755 --- a/bin/coding-standard.sh +++ b/bin/lint.sh @@ -1,7 +1,8 @@ #!/bin/bash PROJECT_ROOT=$(git rev-parse --show-toplevel) -docker run --rm -v "$PROJECT_ROOT:/app" -w /app phpcs:wp --standard=phpcs.xml ./ +docker run --rm -v "$PROJECT_ROOT:/app" -w /app lint:wc -v --standard=phpcs.xml ./ EXIT_CODE=$? + if [[ $EXIT_CODE -ne 0 ]]; then echo "Fix the errors before commit!" exit 1 diff --git a/bin/php-compatibility.sh b/bin/php-compatibility.sh new file mode 100755 index 00000000..73b22572 --- /dev/null +++ b/bin/php-compatibility.sh @@ -0,0 +1,9 @@ +#!/bin/bash +docker run --rm -v "$(pwd)/:/app/" -v "$(pwd)/.php_cs.cache:/app/.php_cs.cache" --entrypoint /composer/vendor/bin/phpcs \ + php-compatibility:wc -p . --standard=PHPCompatibility -s --runtime-set testVersion 5.6-8.4 --ignore=\*/vendor/\*,build/*,node_modules/*,coverage/* +EXIT_CODE=$? + +if [[ $EXIT_CODE -ne 0 ]]; then + echo "Check PHP code compatibility before commit!" + exit 1 +fi diff --git a/composer.json b/composer.json index d117fd39..3eff012f 100644 --- a/composer.json +++ b/composer.json @@ -56,5 +56,11 @@ "automattic/jetpack-autoloader": true }, "lock": false + }, + "repositories": { + "alma/alma-php-client": { + "type": "path", + "url": "/app/alma/alma-php-client" + } } } diff --git a/dockerfile.md b/dockerfile.md index 53979ec5..fa6050fb 100644 --- a/dockerfile.md +++ b/dockerfile.md @@ -14,12 +14,7 @@ Used to build the ZIP file to release the plugin. ## PHPCS Dockerfile -Used to run the `phpcs` command by the pre-commit hook or manually with the command +Used to run the `lint` and `lint:fix` command by the pre-commit hook or manually with the command ```task lint``` - -## PHPCBF Dockerfile - -Used to run the `phpcbf` command by the pre-commit hook or manually with the command - ```task lint:fix``` \ No newline at end of file diff --git a/lint.Dockerfile b/lint.Dockerfile new file mode 100644 index 00000000..560b7aba --- /dev/null +++ b/lint.Dockerfile @@ -0,0 +1,25 @@ +ARG PHP_IMG_TAG=5.6-alpine +FROM php:${PHP_IMG_TAG} AS production + +WORKDIR /composer + +RUN apk add --no-cache composer +RUN composer self-update +RUN composer init -n --name="alma/php-cs" --description="php-cs" --type="library" + +RUN composer config --no-interaction --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true +RUN composer require phpcsstandards/phpcsutils --no-interaction +RUN composer require phpcsstandards/phpcsextra --no-interaction +RUN composer require squizlabs/php_codesniffer --no-interaction +RUN composer require wp-coding-standards/wpcs --no-interaction +RUN composer require phpcompatibility/php-compatibility --no-interaction +RUN composer require phpcompatibility/phpcompatibility-wp:"*" --no-interaction +RUN composer require phpcompatibility/phpcompatibility-paragonie:"*" --no-interaction + +RUN /composer/vendor/bin/phpcbf --config-set installed_paths /app/vendor/phpcsstandards/phpcsutils,/app/vendor/phpcsstandards/phpcsextra,/app/vendor/squizlabs/php_codesniffer,/app/vendor/wp-coding-standards/wpcs,/app/vendor/phpcompatibility/php-compatibility,/app/vendor/phpcompatibility/phpcompatibility-wp,/app/vendor/phpcompatibility/phpcompatibility-paragonie +RUN /composer/vendor/bin/phpcs --config-set installed_paths /app/vendor/phpcsstandards/phpcsutils,/app/vendor/phpcsstandards/phpcsextra,/app/vendor/squizlabs/php_codesniffer,/app/vendor/wp-coding-standards/wpcs,/app/vendor/phpcompatibility/php-compatibility,/app/vendor/phpcompatibility/phpcompatibility-wp,/app/vendor/phpcompatibility/phpcompatibility-paragonie + +WORKDIR /app + +ENTRYPOINT ["/composer/vendor/bin/phpcs"] +CMD ["--version"] \ No newline at end of file diff --git a/phpcbf.Dockerfile b/phpcbf.Dockerfile deleted file mode 100644 index e67be531..00000000 --- a/phpcbf.Dockerfile +++ /dev/null @@ -1,54 +0,0 @@ -ARG PHP_IMG_TAG=5.6-alpine -FROM php:${PHP_IMG_TAG} AS builder - -# Install build dependencies -RUN set -eux \ - && apk add --no-cache \ - ca-certificates \ - # coreutils add 'sort -V' - coreutils \ - curl \ - git \ - && git clone https://github.com/PHPCSStandards/PHP_CodeSniffer - -ARG PBF_VERSION=latest -RUN set -eux \ - && cd PHP_CodeSniffer \ - && if [ "${PBF_VERSION}" = "latest" ]; then \ - VERSION="$( git describe --abbrev=0 --tags )"; \ - else \ - VERSION="$( git tag | grep -E "^v?${PBF_VERSION}\.[.0-9]+\$" | sort -V | tail -1 )"; \ - fi \ - && echo "Version: ${VERSION}" \ - && curl -sS -L https://github.com/PHPCSStandards/PHP_CodeSniffer/releases/download/${VERSION}/phpcbf.phar -o /phpcbf.phar \ - && chmod +x /phpcbf.phar \ - && mv /phpcbf.phar /usr/bin/phpcbf \ - \ - && phpcbf --version - - -ARG PHP_IMG_TAG=5.6-alpine -FROM php:${PHP_IMG_TAG} AS production - -COPY --from=builder /usr/bin/phpcbf /usr/bin/phpcbf - -WORKDIR /app - -RUN apk add --no-cache composer -RUN composer self-update -RUN composer init -n --name="alma/phpcbf" --description="phpcbf" --type="library" -RUN composer config --no-interaction --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -RUN composer require phpcsstandards/phpcsutils --no-interaction -RUN composer require phpcsstandards/phpcsextra --no-interaction -RUN composer require squizlabs/php_codesniffer --no-interaction -RUN composer require wp-coding-standards/wpcs --no-interaction -RUN composer require phpcompatibility/php-compatibility --no-interaction -RUN composer require phpcompatibility/phpcompatibility-wp:"*" --no-interaction -RUN composer require phpcompatibility/phpcompatibility-paragonie:"*" - -RUN phpcbf --config-set installed_paths /app/vendor/phpcsstandards/phpcsutils,/app/vendor/phpcsstandards/phpcsextra,/app/vendor/squizlabs/php_codesniffer,/app/vendor/wp-coding-standards/wpcs,/app/vendor/phpcompatibility/php-compatibility,/app/vendor/phpcompatibility/phpcompatibility-wp,/app/vendor/phpcompatibility/phpcompatibility-paragonie - -COPY phpcs.xml /app/phpcs.xml - -ENTRYPOINT ["phpcbf"] -CMD ["--version"] \ No newline at end of file diff --git a/phpcs.Dockerfile b/phpcs.Dockerfile deleted file mode 100644 index edd53ef3..00000000 --- a/phpcs.Dockerfile +++ /dev/null @@ -1,53 +0,0 @@ -ARG PHP_IMG_TAG=5.6-alpine -FROM php:${PHP_IMG_TAG} AS builder - -# Install build dependencies -RUN set -eux \ - && apk add --no-cache \ - ca-certificates \ - # coreutils add 'sort -V' - coreutils \ - curl \ - git \ - && git clone https://github.com/PHPCSStandards/PHP_CodeSniffer - -ARG PCS_VERSION=latest -RUN set -eux \ - && cd PHP_CodeSniffer \ - && if [ "${PCS_VERSION}" = "latest" ]; then \ - VERSION="$( git describe --abbrev=0 --tags )"; \ - else \ - VERSION="$( git tag | grep -E "^v?${PCS_VERSION}\.[.0-9]+\$" | sort -V | tail -1 )"; \ - fi \ - && echo "Version: ${VERSION}" \ - && curl -sS -L https://github.com/PHPCSStandards/PHP_CodeSniffer/releases/download/${VERSION}/phpcs.phar -o /phpcs.phar \ - && chmod +x /phpcs.phar \ - && mv /phpcs.phar /usr/bin/phpcs \ - \ - && phpcs --version - -ARG PHP_IMG_TAG=5.6-alpine -FROM php:${PHP_IMG_TAG} AS production - -COPY --from=builder /usr/bin/phpcs /usr/bin/phpcs - -WORKDIR /app - -RUN apk add --no-cache composer -RUN composer self-update -RUN composer init -n --name="alma/phpcs" --description="phpcs" --type="library" -RUN composer config --no-interaction --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true -RUN composer require phpcsstandards/phpcsutils --no-interaction -RUN composer require phpcsstandards/phpcsextra --no-interaction -RUN composer require squizlabs/php_codesniffer --no-interaction -RUN composer require wp-coding-standards/wpcs --no-interaction -RUN composer require phpcompatibility/php-compatibility --no-interaction -RUN composer require phpcompatibility/phpcompatibility-wp:"*" --no-interaction -RUN composer require phpcompatibility/phpcompatibility-paragonie:"*" - -RUN phpcs --config-set installed_paths /app/vendor/phpcsstandards/phpcsutils,/app/vendor/phpcsstandards/phpcsextra,/app/vendor/squizlabs/php_codesniffer,/app/vendor/wp-coding-standards/wpcs,/app/vendor/phpcompatibility/php-compatibility,/app/vendor/phpcompatibility/phpcompatibility-wp,/app/vendor/phpcompatibility/phpcompatibility-paragonie - -COPY phpcs.xml /app/phpcs.xml - -ENTRYPOINT ["phpcs"] -CMD ["--version"] \ No newline at end of file From 89465b4a20df8bd0139e79b3f9e250d76a502026 Mon Sep 17 00:00:00 2001 From: Martin Supiot Date: Fri, 21 Feb 2025 18:15:07 +0100 Subject: [PATCH 4/7] fix: clean linter --- bin/lint-fix.sh | 3 +-- bin/lint.sh | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/bin/lint-fix.sh b/bin/lint-fix.sh index d12c80b1..94ea1578 100755 --- a/bin/lint-fix.sh +++ b/bin/lint-fix.sh @@ -1,6 +1,5 @@ #!/bin/bash -PROJECT_ROOT=$(git rev-parse --show-toplevel) -docker run --rm -v "$PROJECT_ROOT:/app" -w /app lint:wc -v --standard=phpcs.xml ./ +docker run --rm -v "$(pwd):/app" -w /app lint:wc -v --standard=phpcs.xml ./ EXIT_CODE=$? if [[ $EXIT_CODE -ne 0 ]]; then diff --git a/bin/lint.sh b/bin/lint.sh index ef9c5256..883dc860 100755 --- a/bin/lint.sh +++ b/bin/lint.sh @@ -1,6 +1,5 @@ #!/bin/bash -PROJECT_ROOT=$(git rev-parse --show-toplevel) -docker run --rm -v "$PROJECT_ROOT:/app" -w /app lint:wc -v --standard=phpcs.xml ./ +docker run --rm -v "$(pwd):/app" -w /app lint:wc -v --standard=phpcs.xml ./ EXIT_CODE=$? if [[ $EXIT_CODE -ne 0 ]]; then From bb790dea778098138362dce0da54d12ac63bbad7 Mon Sep 17 00:00:00 2001 From: Martin Supiot Date: Fri, 21 Feb 2025 18:16:47 +0100 Subject: [PATCH 5/7] fix: clean code --- bin/php-compatibility.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/php-compatibility.sh b/bin/php-compatibility.sh index 73b22572..a3df79ba 100755 --- a/bin/php-compatibility.sh +++ b/bin/php-compatibility.sh @@ -1,5 +1,5 @@ #!/bin/bash -docker run --rm -v "$(pwd)/:/app/" -v "$(pwd)/.php_cs.cache:/app/.php_cs.cache" --entrypoint /composer/vendor/bin/phpcs \ +docker run --rm -v "$(pwd):/app" -v "$(pwd)/.php_cs.cache:/app/.php_cs.cache" --entrypoint /composer/vendor/bin/phpcs \ php-compatibility:wc -p . --standard=PHPCompatibility -s --runtime-set testVersion 5.6-8.4 --ignore=\*/vendor/\*,build/*,node_modules/*,coverage/* EXIT_CODE=$? From 8b2bf876833442bfd057ccff6fe64ab73b2713c9 Mon Sep 17 00:00:00 2001 From: Martin Supiot Date: Mon, 24 Feb 2025 10:45:26 +0100 Subject: [PATCH 6/7] fix: add php version compatibility test --- .pre-commit-config.yaml | 13 +++++++++++-- composer.json | 6 ------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c708b367..83f22b01 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,13 +23,22 @@ repos: - id: coding-standard-fixer name: PHP and WordPress coding standards Fixer test language: system - entry: ./bin/lint-fix.sh + pass_filenames: false + entry: task lint:fix stages: [ pre-commit ] - id: coding-standard name: Check the PHP and WordPress coding standards language: system - entry: ./bin/lint.sh + pass_filenames: false + entry: task lint + stages: [ pre-commit ] + + - id: php-compatibility + name: Check the PHP compatibility + language: system + pass_filenames: false + entry: task php-compatibility stages: [ pre-commit ] # - repo: https://github.com/returntocorp/semgrep diff --git a/composer.json b/composer.json index 3eff012f..d117fd39 100644 --- a/composer.json +++ b/composer.json @@ -56,11 +56,5 @@ "automattic/jetpack-autoloader": true }, "lock": false - }, - "repositories": { - "alma/alma-php-client": { - "type": "path", - "url": "/app/alma/alma-php-client" - } } } From c29d5409e0b6b5b02a3d7170c2257404c1a94911 Mon Sep 17 00:00:00 2001 From: Martin Supiot Date: Mon, 24 Feb 2025 17:40:43 +0100 Subject: [PATCH 7/7] fix linter --- lint.Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lint.Dockerfile b/lint.Dockerfile index 560b7aba..824fa542 100644 --- a/lint.Dockerfile +++ b/lint.Dockerfile @@ -16,8 +16,8 @@ RUN composer require phpcompatibility/php-compatibility --no-interaction RUN composer require phpcompatibility/phpcompatibility-wp:"*" --no-interaction RUN composer require phpcompatibility/phpcompatibility-paragonie:"*" --no-interaction -RUN /composer/vendor/bin/phpcbf --config-set installed_paths /app/vendor/phpcsstandards/phpcsutils,/app/vendor/phpcsstandards/phpcsextra,/app/vendor/squizlabs/php_codesniffer,/app/vendor/wp-coding-standards/wpcs,/app/vendor/phpcompatibility/php-compatibility,/app/vendor/phpcompatibility/phpcompatibility-wp,/app/vendor/phpcompatibility/phpcompatibility-paragonie -RUN /composer/vendor/bin/phpcs --config-set installed_paths /app/vendor/phpcsstandards/phpcsutils,/app/vendor/phpcsstandards/phpcsextra,/app/vendor/squizlabs/php_codesniffer,/app/vendor/wp-coding-standards/wpcs,/app/vendor/phpcompatibility/php-compatibility,/app/vendor/phpcompatibility/phpcompatibility-wp,/app/vendor/phpcompatibility/phpcompatibility-paragonie +RUN /composer/vendor/bin/phpcbf --config-set installed_paths /composer/vendor/phpcsstandards/phpcsutils,/composer/vendor/phpcsstandards/phpcsextra,/composer/vendor/squizlabs/php_codesniffer,/composer/vendor/wp-coding-standards/wpcs,/composer/vendor/phpcompatibility/php-compatibility,/composer/vendor/phpcompatibility/phpcompatibility-wp,/composer/vendor/phpcompatibility/phpcompatibility-paragonie +RUN /composer/vendor/bin/phpcs --config-set installed_paths /composer/vendor/phpcsstandards/phpcsutils,/composer/vendor/phpcsstandards/phpcsextra,/composer/vendor/squizlabs/php_codesniffer,/composer/vendor/wp-coding-standards/wpcs,/composer/vendor/phpcompatibility/php-compatibility,/composer/vendor/phpcompatibility/phpcompatibility-wp,/composer/vendor/phpcompatibility/phpcompatibility-paragonie WORKDIR /app