Skip to content

Commit

Permalink
wip : phpcs wordpress rules partly integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
lgersman committed Nov 22, 2024
1 parent bc4ac81 commit 6f4d958
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ packages/*/*/webpack.config.js
*storybook.log
/playwright/.*/
/phpunit/vendor/
/tmp
53 changes: 41 additions & 12 deletions ecs-config.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php

declare(strict_types=1);

// use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
// use WordPressCS\WordPress\Sniffs\Arrays\ArrayDeclarationSpacingSniff;
use Symplify\EasyCodingStandard\Config\ECSConfig;

// use PhpCsFixer\Fixer\ArrayNotation\TrailingCommaInMultilineArrayFixer;
// $codeSnifferConfig = new PHP_CodeSniffer\Config(["--standard=WordPress"]);
// print_r( $codeSnifferConfig);
// $codeSnifferRuleset = new PHP_CodeSniffer\Ruleset($codeSnifferConfig);

return ECSConfig::configure()
->withPaths([__DIR__])
->withRootFiles()
->withSkip(
['*/vendor/*', '*/build/*', '*/dist/*', '*/node_modules/*', '*/languages/*', '/wp-env-home/*', '/phpunit/*']
['*/vendor/*', '*/build/*', '*/dist/*', '*/node_modules/*', '*/languages/*', '/wp-env-home/*', '/phpunit/*', '/tmp/*']
)
->withPreparedSets(
symplify: true,
Expand All @@ -30,13 +32,40 @@
)
// use 2 spaces instead of psr12 default (4 spaces)
->withSpacing(indentation: ' ')
// // force trailing commas in multiline arrays
// ->withRules([
// TrailingCommaInMultilineArrayFixer::class,
// ]);
/*
->withConfiguredRule(LineLengthFixer::class, [
LineLengthFixer::LINE_LENGTH => 80,
LineLengthFixer::BREAK_LONG_LINES => true,
])*/

/*
// example of adding a single rule without configuration
// force trailing commas in multiline arrays
->withRules([
TrailingCommaInMultilineArrayFixer::class,
]);
*/

/*
// example of adding a single rule with configuration
->withConfiguredRule(LineLengthFixer::class, [
LineLengthFixer::LINE_LENGTH => 80,
LineLengthFixer::BREAK_LONG_LINES => true,
])
*/

/*
// example of importing a WordPress Coding standard rule
->withRules([ArrayDeclarationSpacingSniff::class])
*/

/*
// example of importing a WordPress Coding standard rule with configuration
->withConfiguredRule(ArrayDeclarationSpacingSniff::class, [])
// rulesWithConfiguration
->withRules([
// our existing PSR12 set from PHP Code Sniffer
...array_values($codeSnifferRuleset->sniffCodes),
// // and the two new rules I wanted from PHP CS Fixer
// PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer::class,
// PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer::class,
])
*/
;
1 change: 1 addition & 0 deletions packages/docker/ecs-php/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ECS_VERSION=12.2.1
PHP_VERSION=8.3
WORDPRESS_CODING_STANDARDS_VERSION=3.1
23 changes: 14 additions & 9 deletions packages/docker/ecs-php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,39 @@
# php version from .env
ARG PHP_VERSION

FROM php:${PHP_VERSION}-cli as base

FROM php:${PHP_VERSION}-cli-alpine as build
COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN composer --version

# ecs version from .env
ARG ECS_VERSION
ARG WORDPRESS_CODING_STANDARDS_VERSION

RUN echo "WORDPRESS_CODING_STANDARDS_VERSION=${WORDPRESS_CODING_STANDARDS_VERSION}"

ENV COMPOSER_HOME=/tmp
ENV COMPOSER_HOME=/composer
ENV COMPOSER_ALLOW_SUPERUSER=1

RUN composer global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer false \
&& composer global require symplify/easy-coding-standard:${ECS_VERSION} wp-coding-standards/wpcs --with-dependencies
RUN composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true \
&& composer global require --dev wp-coding-standards/wpcs:${WORDPRESS_CODING_STANDARDS_VERSION}

FROM php:${PHP_VERSION}-cli-alpine
RUN composer global config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer false \
&& composer global require symplify/easy-coding-standard:${ECS_VERSION} --with-dependencies

# install apk packages to create a user
# install i18n support into php
RUN apk add --no-cache icu-dev shadow && docker-php-ext-install intl

COPY --from=build /tmp/vendor/ /composer/
COPY --chmod=755 <<EOF /usr/bin/entrypoint.sh
#!/bin/sh

exec /composer/bin/ecs \$@
exec /composer/vendor/bin/ecs \$@
EOF

ENV PATH="${PATH}:/composer/bin/"
COPY packages/docker/ecs-php/ruleset.xml /ruleset.xml

ENV PATH="${PATH}:/composer/vendor/bin"

# create user/group to run ecs as non-root
RUN groupadd --gid 1000 php && useradd --uid 1000 --gid php --shell /bin/sh --create-home php
Expand Down
38 changes: 38 additions & 0 deletions packages/docker/ecs-php/ruleset.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0"?>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="ionos-wp-dev"
namespace="ionos\wp-dev"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd"
>

<description>IONOS wp-dev PHP Coding Standard</description>

<!-- ignore the following monorepo dirs/files -->
<exclude-pattern>*/dist/*</exclude-pattern>
<exclude-pattern>*/build/*</exclude-pattern>
<exclude-pattern>./tmp/*</exclude-pattern>
<exclude-pattern>./phpunit/*</exclude-pattern>
<exclude-pattern>./wp-env-home/*</exclude-pattern>
<exclude-pattern>*/languages/*</exclude-pattern>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<!-- we don't want to check our php config files against wordpress rules -->
<exclude-pattern>./rector-config-php7.4.php</exclude-pattern>
<exclude-pattern>./ecs-config.php</exclude-pattern>

<arg name="extensions" value="php" />

<!--
Included via WordPress-Extra.
<rule ref="WordPress-Core"/>
-->
<rule ref="WordPress-Extra">
<exclude name="Squiz"/>
<exclude name="PEAR"/>
<exclude name="Universal"/>
<exclude name="Generic"/>
</rule>

</ruleset>

0 comments on commit 6f4d958

Please sign in to comment.