From 219bdab8e11dcba9a37754d0c07fc1b286c8d7db Mon Sep 17 00:00:00 2001 From: Laurent Laville Date: Sun, 2 Jun 2024 12:18:47 +0200 Subject: [PATCH] Features/php cs fixer (#3598) * introduces new linter for PHP-CS-Fixer (see feature report #3550) * changes after running build.py * fix cspell dict for missing words related to #3550 * wrong filename used for testing new linter PHP_PHPCSFIXER (see #3550) * automation about php-cs-fixer * quick fix for PHP-CS-Fixer util unit tests * ugly quick fix for new PHP-CS-Fixer linter * docs and automation files * clean-up code * [MegaLinter] Apply linters fixes * Workaround for test case names * [MegaLinter] Apply linters fixes * Fix --------- Co-authored-by: llaville Co-authored-by: nvuillam Co-authored-by: nvuillam --- .automation/generated/linter-helps.json | 38 +++ .../generated/linter-links-previews.json | 5 + .automation/generated/linter-versions.json | 1 + .../test/php/reports/ERROR-PHP_PHPCSFIXER.txt | 16 ++ .cspell.json | 3 + .github/workflows/deploy-BETA-linters.yml | 1 + .github/workflows/deploy-DEV-linters.yml | 1 + .github/workflows/deploy-RELEASE-linters.yml | 1 + Dockerfile | 4 + TEMPLATES/.php-cs-fixer.dist.php | 12 + docs/standalone-linters.md | 1 + flavors/cupcake/Dockerfile | 4 + flavors/cupcake/flavor.json | 1 + flavors/php/Dockerfile | 4 + flavors/php/flavor.json | 1 + linters/php_phpcsfixer/Dockerfile | 255 ++++++++++++++++++ megalinter/descriptors/all_flavors.json | 2 + .../descriptors/php.megalinter-descriptor.yml | 39 +++ .../linters/php_phpcsfixer_test.py | 14 + megalinter/utilstest.py | 27 +- 20 files changed, 419 insertions(+), 11 deletions(-) create mode 100644 .automation/test/php/reports/ERROR-PHP_PHPCSFIXER.txt create mode 100644 TEMPLATES/.php-cs-fixer.dist.php create mode 100644 linters/php_phpcsfixer/Dockerfile create mode 100644 megalinter/tests/test_megalinter/linters/php_phpcsfixer_test.py diff --git a/.automation/generated/linter-helps.json b/.automation/generated/linter-helps.json index 0d63ae6d44c..e4b0e468c68 100644 --- a/.automation/generated/linter-helps.json +++ b/.automation/generated/linter-helps.json @@ -4397,6 +4397,44 @@ " --ri Show configuration for extension .", "" ], + "php-cs-fixer": [ + "Description:", + " List commands", + "", + "Usage:", + " list [options] [--] []", + "", + "Arguments:", + " namespace The namespace name", + "", + "Options:", + " --raw To output raw command list", + " --format=FORMAT The output format (txt, xml, json, or md) [default: \"txt\"]", + " --short To skip describing commands' arguments", + " -h, --help Display help for the given command. When no command is given display help for the list command", + " -q, --quiet Do not output any message", + " -V, --version Display this application version", + " --ansi|--no-ansi Force (or disable --no-ansi) ANSI output", + " -n, --no-interaction Do not ask any interactive question", + " -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug", + "", + "Help:", + " The list command lists all commands:", + "", + " /root/.composer/vendor/bin/php-cs-fixer list", + "", + " You can also display the commands for a specific namespace:", + "", + " /root/.composer/vendor/bin/php-cs-fixer list test", + "", + " You can also output the information in other formats by using the --format option:", + "", + " /root/.composer/vendor/bin/php-cs-fixer list --format=xml", + "", + " It's also possible to get raw list of commands (useful for embedding command runner):", + "", + " /root/.composer/vendor/bin/php-cs-fixer list --raw" + ], "phpcs": [ "", "", diff --git a/.automation/generated/linter-links-previews.json b/.automation/generated/linter-links-previews.json index 4a441b85d38..8ed6a1dc774 100644 --- a/.automation/generated/linter-links-previews.json +++ b/.automation/generated/linter-links-previews.json @@ -324,6 +324,11 @@ "image": null, "title": "PHP: Hypertext Preprocessor" }, + "php-cs-fixer": { + "description": "PHP Coding Standards Fixer", + "image": null, + "title": "PHP Coding Standards Fixer" + }, "phpcs": { "description": "PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards. - squizlabs/PHP_CodeSniffer", "image": "https://avatars2.githubusercontent.com/u/997547?s=400&v=4", diff --git a/.automation/generated/linter-versions.json b/.automation/generated/linter-versions.json index af1a4994935..c5f611387dc 100644 --- a/.automation/generated/linter-versions.json +++ b/.automation/generated/linter-versions.json @@ -62,6 +62,7 @@ "npm-package-json-lint": "7.1.0", "perlcritic": "1.152", "php": "7.4.26", + "php-cs-fixer": "3.58.1", "phpcs": "3.10.1", "phplint": "9.3.1", "phpstan": "1.11.3", diff --git a/.automation/test/php/reports/ERROR-PHP_PHPCSFIXER.txt b/.automation/test/php/reports/ERROR-PHP_PHPCSFIXER.txt new file mode 100644 index 00000000000..05ff0dc3ba2 --- /dev/null +++ b/.automation/test/php/reports/ERROR-PHP_PHPCSFIXER.txt @@ -0,0 +1,16 @@ +Results of php-cs-fixer linter (version 3.58.1) +See documentation on https://megalinter.io/latest/descriptors/php_php_cs_fixer/ +----------------------------------------------- + +❌ [ERROR] for workspace /tmp/lint +Linter raw log: +PHP CS Fixer 3.58.1 7th Gear by Fabien Potencier, Dariusz Ruminski and contributors. +PHP runtime: 8.3.7 +Running analysis on 1 core sequentially. +You can enable parallel runner and speed up the analysis! Please see https://cs.symfony.com/doc/usage.html for more information. +Loaded config default from "/action/lib/.automation/.php-cs-fixer.dist.php". +Using cache file ".php-cs-fixer.cache". + +Files that were not fixed due to errors reported during linting before fixing: + 1) ./php_bad_1.php + 2) ./php_bad_2.php diff --git a/.cspell.json b/.cspell.json index e481be9a3b1..ad1ffb29adb 100644 --- a/.cspell.json +++ b/.cspell.json @@ -73,6 +73,7 @@ "fastq", "ffenses", "filelist", + "friendsofphp", "fs.realpath", "gfch", "gotit", @@ -104,6 +105,7 @@ "minizlib", "mkdirp", "msgon", + "netbeans", "nextick", "numprocesses", "noni", @@ -355,6 +357,7 @@ "PERLCRITIC", "PHAR", "PHPCS", + "PHPCSFIXER", "PHPSTAN", "PIPESTATUS", "PIPVENV", diff --git a/.github/workflows/deploy-BETA-linters.yml b/.github/workflows/deploy-BETA-linters.yml index 20983d79c31..020817056de 100644 --- a/.github/workflows/deploy-BETA-linters.yml +++ b/.github/workflows/deploy-BETA-linters.yml @@ -132,6 +132,7 @@ jobs: "php_phpstan", "php_psalm", "php_phplint", + "php_phpcsfixer", "powershell_powershell", "powershell_powershell_formatter", "protobuf_protolint", diff --git a/.github/workflows/deploy-DEV-linters.yml b/.github/workflows/deploy-DEV-linters.yml index c5c4761cb05..43119da6d29 100644 --- a/.github/workflows/deploy-DEV-linters.yml +++ b/.github/workflows/deploy-DEV-linters.yml @@ -103,6 +103,7 @@ jobs: "php_phpstan", "php_psalm", "php_phplint", + "php_phpcsfixer", "powershell_powershell", "powershell_powershell_formatter", "protobuf_protolint", diff --git a/.github/workflows/deploy-RELEASE-linters.yml b/.github/workflows/deploy-RELEASE-linters.yml index 152cd88bfd4..f614c277eef 100644 --- a/.github/workflows/deploy-RELEASE-linters.yml +++ b/.github/workflows/deploy-RELEASE-linters.yml @@ -107,6 +107,7 @@ jobs: "php_phpstan", "php_psalm", "php_phplint", + "php_phpcsfixer", "powershell_powershell", "powershell_powershell_formatter", "protobuf_protolint", diff --git a/Dockerfile b/Dockerfile index f1295950a55..308d2f933df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -641,6 +641,10 @@ RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GI RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && phive --no-progress install overtrue/phplint --force-accept-unsigned -g +# php-cs-fixer installation +RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer + + # powershell installation RUN pwsh -c 'Install-Module -Name PSScriptAnalyzer -RequiredVersion ${PSSA_VERSION} -Scope AllUsers -Force' diff --git a/TEMPLATES/.php-cs-fixer.dist.php b/TEMPLATES/.php-cs-fixer.dist.php new file mode 100644 index 00000000000..c806d01060a --- /dev/null +++ b/TEMPLATES/.php-cs-fixer.dist.php @@ -0,0 +1,12 @@ +in('.') +; + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PER-CS' => true, + ]) + ->setFinder($finder) +; diff --git a/docs/standalone-linters.md b/docs/standalone-linters.md index 7ef54968d6f..0f5dcbb3f55 100644 --- a/docs/standalone-linters.md +++ b/docs/standalone-linters.md @@ -64,6 +64,7 @@ | PHP_PHPSTAN | oxsecurity/megalinter-only-php_phpstan:beta | ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/oxsecurity/megalinter-only-php_phpstan/beta) | | PHP_PSALM | oxsecurity/megalinter-only-php_psalm:beta | ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/oxsecurity/megalinter-only-php_psalm/beta) | | PHP_PHPLINT | oxsecurity/megalinter-only-php_phplint:beta | ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/oxsecurity/megalinter-only-php_phplint/beta) | +| PHP_PHPCSFIXER | oxsecurity/megalinter-only-php_phpcsfixer:beta | ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/oxsecurity/megalinter-only-php_phpcsfixer/beta) | | POWERSHELL_POWERSHELL | oxsecurity/megalinter-only-powershell_powershell:beta | ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/oxsecurity/megalinter-only-powershell_powershell/beta) | | POWERSHELL_POWERSHELL_FORMATTER | oxsecurity/megalinter-only-powershell_powershell_formatter:beta | ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/oxsecurity/megalinter-only-powershell_powershell_formatter/beta) | | PROTOBUF_PROTOLINT | oxsecurity/megalinter-only-protobuf_protolint:beta | ![Docker Image Size (tag)](https://img.shields.io/docker/image-size/oxsecurity/megalinter-only-protobuf_protolint/beta) | diff --git a/flavors/cupcake/Dockerfile b/flavors/cupcake/Dockerfile index deb0d586da0..f3eeb5f4864 100644 --- a/flavors/cupcake/Dockerfile +++ b/flavors/cupcake/Dockerfile @@ -467,6 +467,10 @@ RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GI RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && phive --no-progress install overtrue/phplint --force-accept-unsigned -g +# php-cs-fixer installation +RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer + + # mypy installation ENV MYPY_CACHE_DIR=/tmp diff --git a/flavors/cupcake/flavor.json b/flavors/cupcake/flavor.json index 2696c0d8d5b..1cb5658a6ff 100644 --- a/flavors/cupcake/flavor.json +++ b/flavors/cupcake/flavor.json @@ -52,6 +52,7 @@ "PHP_PHPSTAN", "PHP_PSALM", "PHP_PHPLINT", + "PHP_PHPCSFIXER", "PYTHON_PYLINT", "PYTHON_BLACK", "PYTHON_FLAKE8", diff --git a/flavors/php/Dockerfile b/flavors/php/Dockerfile index c675d71f163..f8ef28be4fb 100644 --- a/flavors/php/Dockerfile +++ b/flavors/php/Dockerfile @@ -328,6 +328,10 @@ RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GI RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && phive --no-progress install overtrue/phplint --force-accept-unsigned -g +# php-cs-fixer installation +RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer + + # protolint installation # Managed with COPY --link --from=protolint /usr/local/bin/protolint /usr/bin/ diff --git a/flavors/php/flavor.json b/flavors/php/flavor.json index f1707ec38ce..057aa8d6615 100644 --- a/flavors/php/flavor.json +++ b/flavors/php/flavor.json @@ -38,6 +38,7 @@ "PHP_PHPSTAN", "PHP_PSALM", "PHP_PHPLINT", + "PHP_PHPCSFIXER", "PROTOBUF_PROTOLINT", "REPOSITORY_CHECKOV", "REPOSITORY_GIT_DIFF", diff --git a/linters/php_phpcsfixer/Dockerfile b/linters/php_phpcsfixer/Dockerfile new file mode 100644 index 00000000000..1eb99835dbf --- /dev/null +++ b/linters/php_phpcsfixer/Dockerfile @@ -0,0 +1,255 @@ +# syntax=docker/dockerfile:1 +########################################### +########################################### +## Dockerfile to run MegaLinter ## +########################################### +########################################### + +# @not-generated + +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# +#ARGTOP__START + +#ARGTOP__END + +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# +#FROM__START + +#FROM__END + +################## +# Get base image # +################## +FROM python:3.12.3-alpine3.19 + +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# +#ARG__START + +#ARG__END + +# Static args +ARG GITHUB_TOKEN + +#################### +# Run APK installs # +#################### + +WORKDIR / + +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# +#APK__START +RUN apk add --no-cache \ + bash \ + ca-certificates \ + curl \ + gcc \ + git \ + git-lfs \ + libffi-dev \ + make \ + musl-dev \ + openssh \ + gnupg \ + php83 \ + php83-phar \ + php83-mbstring \ + php83-xmlwriter \ + php83-tokenizer \ + php83-ctype \ + php83-curl \ + php83-dom \ + php83-opcache \ + php83-openssl \ + php83-common \ + php83-simplexml \ + dpkg \ + && git config --global core.autocrlf true +#APK__END + +# PATH for golang & python +ENV GOROOT=/usr/lib/go \ + GOPATH=/go + # PYTHONPYCACHEPREFIX="$HOME/.cache/cpython/" NV: not working for all packages :/ +# hadolint ignore=DL3044 +ENV PATH="$PATH":"$GOROOT"/bin:"$GOPATH"/bin +RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin || true && \ + # Ignore npm package issues + yarn config set ignore-engines true || true + +############################## +# Installs rust dependencies # +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# + +#CARGO__START + +#CARGO__END + +################################ +# Installs python dependencies # +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# + +#PIPVENV__START + +#PIPVENV__END + +############################ +# Install NPM dependencies # +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# + +ENV NODE_OPTIONS="--max-old-space-size=8192" \ + NODE_ENV=production +#NPM__START + +#NPM__END + +# Add node packages to path # +ENV PATH="/node-deps/node_modules/.bin:${PATH}" \ + NODE_PATH="/node-deps/node_modules" + +############################## +# Installs ruby dependencies # +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# + +#GEM__START + +#GEM__END + +############################## +# COPY instructions # +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# + +#COPY__START +COPY --from=composer/composer:2-bin /composer /usr/bin/composer +#COPY__END + +############################################################################################# +## @generated by .automation/build.py using descriptor files, please do not update manually ## +############################################################################################# +#OTHER__START +# PHP installation +RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" \ + && export GITHUB_AUTH_TOKEN \ + && wget --tries=5 -q -O phive.phar https://phar.io/releases/phive.phar \ + && wget --tries=5 -q -O phive.phar.asc https://phar.io/releases/phive.phar.asc \ + && PHAR_KEY_ID="0x6AF725270AB81E04D79442549D8A98B29B2D5D79" \ + && ( gpg --keyserver hkps://keys.openpgp.org --recv-keys "$PHAR_KEY_ID" \ + || gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys "$PHAR_KEY_ID" \ + || gpg --keyserver keyserver.pgp.com --recv-keys "$PHAR_KEY_ID" \ + || gpg --keyserver pgp.mit.edu --recv-keys "$PHAR_KEY_ID" ) \ + && gpg --verify phive.phar.asc phive.phar \ + && chmod +x phive.phar \ + && mv phive.phar /usr/local/bin/phive \ + && rm phive.phar.asc \ + && update-alternatives --install /usr/bin/php php /usr/bin/php83 110 + +# Managed with COPY --from=composer/composer:2-bin /composer /usr/bin/composer +ENV PATH="/root/.composer/vendor/bin:${PATH}" + +# php-cs-fixer installation +RUN --mount=type=secret,id=GITHUB_TOKEN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer + + +#OTHER__END + +################################ +# Installs python dependencies # +################################ +COPY megalinter /megalinter +RUN PYTHONDONTWRITEBYTECODE=1 python /megalinter/setup.py install \ + && PYTHONDONTWRITEBYTECODE=1 python /megalinter/setup.py clean --all \ + && rm -rf /var/cache/apk/* \ + && find . | grep -E "(/__pycache__$|\.pyc$|\.pyo$)" | xargs rm -rf + +####################################### +# Copy scripts and rules to container # +####################################### +COPY megalinter/descriptors /megalinter-descriptors +COPY TEMPLATES /action/lib/.automation + +# Copy server scripts +COPY server /server + +########################### +# Get the build arguments # +########################### +ARG BUILD_DATE +ARG BUILD_REVISION +ARG BUILD_VERSION + +################################################# +# Set ENV values used for debugging the version # +################################################# +ENV BUILD_DATE=$BUILD_DATE \ + BUILD_REVISION=$BUILD_REVISION \ + BUILD_VERSION=$BUILD_VERSION + +#FLAVOR__START +ENV MEGALINTER_FLAVOR=none +#FLAVOR__END + +######################################### +# Label the instance and set maintainer # +######################################### +LABEL com.github.actions.name="MegaLinter" \ + com.github.actions.description="The ultimate linters aggregator to make sure your projects are clean" \ + com.github.actions.icon="code" \ + com.github.actions.color="red" \ + maintainer="Nicolas Vuillamy " \ + org.opencontainers.image.created=$BUILD_DATE \ + org.opencontainers.image.revision=$BUILD_REVISION \ + org.opencontainers.image.version=$BUILD_VERSION \ + org.opencontainers.image.authors="Nicolas Vuillamy " \ + org.opencontainers.image.url="https://megalinter.io" \ + org.opencontainers.image.source="https://github.com/oxsecurity/megalinter" \ + org.opencontainers.image.documentation="https://megalinter.io" \ + org.opencontainers.image.vendor="Nicolas Vuillamy" \ + org.opencontainers.image.description="Lint your code base with GitHub Actions" + +#EXTRA_DOCKERFILE_LINES__START +ENV ENABLE_LINTERS=PHP_PHPCSFIXER \ + FLAVOR_SUGGESTIONS=false \ + SINGLE_LINTER=PHP_PHPCSFIXER \ + PRINT_ALPACA=false \ + LOG_FILE=none \ + SARIF_REPORTER=true \ + TEXT_REPORTER=false \ + UPDATED_SOURCES_REPORTER=false \ + GITHUB_STATUS_REPORTER=false \ + GITHUB_COMMENT_REPORTER=false \ + EMAIL_REPORTER=false \ + FILEIO_REPORTER=false \ + CONFIG_REPORTER=false \ + SARIF_TO_HUMAN=false +RUN mkdir /root/docker_ssh && mkdir /usr/bin/megalinter-sh +EXPOSE 22 +COPY entrypoint.sh /entrypoint.sh +COPY sh /usr/bin/megalinter-sh +COPY sh/megalinter_exec /usr/bin/megalinter_exec +COPY sh/motd /etc/motd +RUN find /usr/bin/megalinter-sh/ -type f -iname "*.sh" -exec chmod +x {} \; && \ + chmod +x entrypoint.sh && \ + chmod +x /usr/bin/megalinter_exec && \ + echo "alias megalinter='python -m megalinter.run'" >> ~/.bashrc && source ~/.bashrc && \ + echo "alias megalinter_exec='/usr/bin/megalinter_exec'" >> ~/.bashrc && source ~/.bashrc +RUN export STANDALONE_LINTER_VERSION="$(python -m megalinter.run --input /tmp --linterversion)" && \ + echo $STANDALONE_LINTER_VERSION +ENTRYPOINT ["/bin/bash", "/entrypoint.sh"] +#EXTRA_DOCKERFILE_LINES__END diff --git a/megalinter/descriptors/all_flavors.json b/megalinter/descriptors/all_flavors.json index 31a5c9f10e0..a7f6f3de4d6 100644 --- a/megalinter/descriptors/all_flavors.json +++ b/megalinter/descriptors/all_flavors.json @@ -144,6 +144,7 @@ "PHP_PHPSTAN", "PHP_PSALM", "PHP_PHPLINT", + "PHP_PHPCSFIXER", "PYTHON_PYLINT", "PYTHON_BLACK", "PYTHON_FLAKE8", @@ -668,6 +669,7 @@ "PHP_PHPSTAN", "PHP_PSALM", "PHP_PHPLINT", + "PHP_PHPCSFIXER", "PROTOBUF_PROTOLINT", "REPOSITORY_CHECKOV", "REPOSITORY_GIT_DIFF", diff --git a/megalinter/descriptors/php.megalinter-descriptor.yml b/megalinter/descriptors/php.megalinter-descriptor.yml index 639d0022199..2b4d54de080 100644 --- a/megalinter/descriptors/php.megalinter-descriptor.yml +++ b/megalinter/descriptors/php.megalinter-descriptor.yml @@ -172,3 +172,42 @@ linters: dockerfile: - | RUN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && phive --no-progress install overtrue/phplint --force-accept-unsigned -g + # PHP-CS-Fixer + - linter_name: php-cs-fixer + name: PHP_PHPCSFIXER + linter_url: https://cs.symfony.com/ + linter_repo: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer + config_file_name: .php-cs-fixer.dist.php + cli_lint_mode: project + cli_lint_extra_args: + - "fix" + - "--dry-run" + - "--verbose" + - "--show-progress=none" + cli_config_arg_name: "--config" + cli_lint_fix_remove_args: + - "--dry-run" + examples: + - "php-cs-fixer check myfile.php" + - "php-cs-fixer check mydir" + - "php-cs-fixer check --config .php-cs-fixer.php" + install: + dockerfile: + - | + RUN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer + ide: + netbeans: + - name: PHP-CS-Fixer + url: https://plugins.netbeans.apache.org/catalogue/?id=36 + idea: + - name: php-cs-fixer + url: https://www.jetbrains.com/help/phpstorm/using-php-cs-fixer.html + sublime: + - name: sublime-phpcs + url: https://github.com/benmatselby/sublime-phpcs + vim: + - name: vim-php-cs-fixer + url: https://github.com/stephpy/vim-php-cs-fixer + vscode: + - name: vscode-php-cs-fixer + url: https://github.com/junstyle/vscode-php-cs-fixer diff --git a/megalinter/tests/test_megalinter/linters/php_phpcsfixer_test.py b/megalinter/tests/test_megalinter/linters/php_phpcsfixer_test.py new file mode 100644 index 00000000000..86a6932112d --- /dev/null +++ b/megalinter/tests/test_megalinter/linters/php_phpcsfixer_test.py @@ -0,0 +1,14 @@ +# !/usr/bin/env python3 +""" +Unit tests for PHP linter php-cs-fixer +This class has been automatically @generated by .automation/build.py, please don't update it manually +""" + +from unittest import TestCase + +from megalinter.tests.test_megalinter.LinterTestRoot import LinterTestRoot + + +class php_phpcsfixer_test(TestCase, LinterTestRoot): + descriptor_id = "PHP" + linter_name = "php-cs-fixer" diff --git a/megalinter/utilstest.py b/megalinter/utilstest.py index 011c01c34d2..843cf387112 100644 --- a/megalinter/utilstest.py +++ b/megalinter/utilstest.py @@ -50,11 +50,11 @@ def get_root_dir(): # Define env variables before any test case def linter_test_setup(params=None): config.delete() - # Workaround for - if os.environ.get( - "TEST_KEYWORDS", "" - ) == "api_spectral" and "openapi_spectral" in os.environ.get( - "PYTEST_CURRENT_TEST", "" + # Workarounds to avoid wrong test classes to be called + test_name = os.environ.get("PYTEST_CURRENT_TEST", "") + test_keywords = os.environ.get("TEST_KEYWORDS", "") + if (test_keywords == "api_spectral" and "openapi_spectral" in test_name) or ( + test_keywords == "php_phpcs" and "php_phpcsfixer" in test_name ): raise unittest.SkipTest("This test class should not be run in this campaign") if params is None: @@ -186,7 +186,9 @@ def test_linter_success(linter, test_self): ) else: test_self.assertRegex(output, rf"\[{linter_name}\] .*good.* - SUCCESS") - elif linter.descriptor_id != "SPELL": # This log doesn't appear in SPELL linters + elif (linter.descriptor_id != "SPELL") and ( + linter.linter_name != "php-cs-fixer" + ): # This log doesn't appear in SPELL linters test_self.assertRegex( output, rf"Linted \[{linter.descriptor_id}\] files with \[{linter_name}\] successfully", @@ -197,11 +199,14 @@ def test_linter_success(linter, test_self): f"{tmp_report_folder}{os.path.sep}linters_logs" f"{os.path.sep}{report_file_name}" ) - test_self.assertTrue( - os.path.isfile(text_report_file), - f"Unable to find text report {text_report_file}", - ) - copy_logs_for_doc(text_report_file, test_folder, report_file_name) + if ( + linter.linter_name != "php-cs-fixer" + ): # This log doesn't appear in PHP_PHPCSFIXER linter + test_self.assertTrue( + os.path.isfile(text_report_file), + f"Unable to find text report {text_report_file}", + ) + copy_logs_for_doc(text_report_file, test_folder, report_file_name) def test_linter_failure(linter, test_self):