Merge pull request #477 from WyriHaximus-labs/add-phpstan

Add PHPStan support
This commit is contained in:
Lukas Gravley 2020-07-29 07:56:49 -05:00 committed by GitHub
commit d1f140283c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 4 deletions

View file

@ -0,0 +1,4 @@
<?php
function f(int $i){}
f("not int');

View file

@ -1,7 +1,11 @@
TAP version 13 TAP version 13
1..2 1..3
not ok 1 - php_bad_1.php not ok 1 - php_bad_1.php
--- ---
message: PHP Parse error syntax error, unexpected 'pe98y' (T_STRING) in /tmp/lint/.automation/test/php/php_bad_1.php on line 3\nErrors parsing /tmp/lint/.automation/test/php/php_bad_1.php\n message: PHP Parse error syntax error, unexpected 'pe98y' (T_STRING) in /tmp/lint/.automation/test/php/php_bad_1.php on line 3\nErrors parsing /tmp/lint/.automation/test/php/php_bad_1.php\n
... ...
ok 2 - php_good_1.php not ok 2 - php_bad_2.php
---
message: PHP Parse error syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in /tmp/lint/.automation/test/php/php_bad_2.php on line 5\nErrors parsing /tmp/lint/.automation/test/php/php_bad_2.php\n
...
ok 3 - php_good_1.php

View file

@ -0,0 +1,11 @@
TAP version 13
1..3
not ok 1 - php_bad_1.php
---
message: ------ ---------------------------------------------- \n Line php_bad_1.php \n ------ ---------------------------------------------- \n 3 Invalid numeric literal on line 3 \n 3 Invalid numeric literal on line 3 \n 3 Syntax error, unexpected '=' on line 3 \n 3 Syntax error, unexpected T_LNUMBER on line 3 \n 3 Syntax error, unexpected T_STRING on line 3 \n 3 Syntax error, unexpected T_STRING on line 3 \n ------ ---------------------------------------------- \n\n [ERROR] Found 6 errors \n
...
not ok 2 - php_bad_2.php
---
message: ------ ------------------------------------------------------- \n Line php_bad_2.php \n ------ ------------------------------------------------------- \n 5 Syntax error, unexpected EOF, expecting T_VARIABLE or \n T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN on line 5 \n ------ ------------------------------------------------------- \n\n [ERROR] Found 1 error \n
...
ok 3 - php_good_1.php

View file

@ -67,9 +67,17 @@ RUN apk add --update --no-cache \
openjdk8-jre \ openjdk8-jre \
perl \ perl \
php7 \ php7 \
php7-phar \
php7-json \
php7-mbstring \
php7-tokenizer \
php7-ctype \
php7-curl \
php7-dom \
py3-setuptools \ py3-setuptools \
readline-dev \ readline-dev \
ruby ruby-dev ruby-bundler ruby-rdoc ruby ruby-dev ruby-bundler ruby-rdoc \
gnupg
######################################## ########################################
# Copy dependencies files to container # # Copy dependencies files to container #
@ -204,6 +212,17 @@ RUN wget https://github.com/cvega/luarocks/archive/v3.3.1-super-linter.tar.gz -O
RUN luarocks install luacheck RUN luarocks install luacheck
#############################
# Install Phive and PHPStan #
#############################
RUN wget -O phive.phar https://phar.io/releases/phive.phar \
&& wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc \
&& gpg --keyserver pool.sks-keyservers.net --recv-keys 0x9D8A98B29B2D5D79 \
&& gpg --verify phive.phar.asc phive.phar \
&& chmod +x phive.phar \
&& mv phive.phar /usr/local/bin/phive \
&& yes | phive install -g phpstan
########################################### ###########################################
# Load GitHub Env Vars for GitHub Actions # # Load GitHub Env Vars for GitHub Actions #
########################################### ###########################################
@ -244,6 +263,7 @@ ENV ACTIONS_RUNNER_DEBUG=${ACTIONS_RUNNER_DEBUG} \
VALIDATE_OPENAPI=${VALIDATE_OPENAPI} \ VALIDATE_OPENAPI=${VALIDATE_OPENAPI} \
VALIDATE_PERL=${VALIDATE_PERL} \ VALIDATE_PERL=${VALIDATE_PERL} \
VALIDATE_PHP=${VALIDATE_PHP} \ VALIDATE_PHP=${VALIDATE_PHP} \
VALIDATE_PHP_PHPSTAN=${VALIDATE_PHP_PHPSTAN} \
VALIDATE_POWERSHELL=${VALIDATE_POWERSHELL} \ VALIDATE_POWERSHELL=${VALIDATE_POWERSHELL} \
VALIDATE_PROTOBUF=${VALIDATE_PROTOBUF} \ VALIDATE_PROTOBUF=${VALIDATE_PROTOBUF} \
VALIDATE_PYTHON=${VALIDATE_PYTHON} \ VALIDATE_PYTHON=${VALIDATE_PYTHON} \

View file

@ -209,6 +209,7 @@ and won't run anything unexpected.
| **VALIDATE_OPENAPI** | `true` | Flag to enable or disable the linting process of the OpenAPI language. | | **VALIDATE_OPENAPI** | `true` | Flag to enable or disable the linting process of the OpenAPI language. |
| **VALIDATE_PERL** | `true` | Flag to enable or disable the linting process of the Perl language. | | **VALIDATE_PERL** | `true` | Flag to enable or disable the linting process of the Perl language. |
| **VALIDATE_PHP** | `true` | Flag to enable or disable the linting process of the PHP language. | | **VALIDATE_PHP** | `true` | Flag to enable or disable the linting process of the PHP language. |
| **VALIDATE_PHP_PHPSTAN** | `true` | Flag to enable or disable the linting process of the PHP language (Utilizing: PHPStan). |
| **VALIDATE_PROTOBUF** | `true` | Flag to enable or disable the linting process of the Protobuf language. | | **VALIDATE_PROTOBUF** | `true` | Flag to enable or disable the linting process of the Protobuf language. |
| **VALIDATE_PYTHON** | `true` | Flag to enable or disable the linting process of the Python language. (Utilizing: pylint) (keep for backward compatibility) | | **VALIDATE_PYTHON** | `true` | Flag to enable or disable the linting process of the Python language. (Utilizing: pylint) (keep for backward compatibility) |
| **VALIDATE_PYTHON_PYLINT** | `true` | Flag to enable or disable the linting process of the Python language. (Utilizing: pylint) | | **VALIDATE_PYTHON_PYLINT** | `true` | Flag to enable or disable the linting process of the Python language. (Utilizing: pylint) |

2
TEMPLATES/phpstan.neon Normal file
View file

@ -0,0 +1,2 @@
parameters:
level: 5

View file

@ -240,6 +240,7 @@ function BuildFileList() {
# Append the file to the array # # Append the file to the array #
################################ ################################
FILE_ARRAY_PHP+=("${FILE}") FILE_ARRAY_PHP+=("${FILE}")
FILE_ARRAY_PHP_PHPSTAN+=("${FILE}")
########################################################## ##########################################################
# Set the READ_ONLY_CHANGE_FLAG since this could be exec # # Set the READ_ONLY_CHANGE_FLAG since this could be exec #
########################################################## ##########################################################

View file

@ -71,6 +71,12 @@ MARKDOWN_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${MARKDOWN_FILE_NAME}"
# OpenAPI Vars # OpenAPI Vars
OPENAPI_FILE_NAME='.openapirc.yml' # Name of the file OPENAPI_FILE_NAME='.openapirc.yml' # Name of the file
OPENAPI_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${OPENAPI_FILE_NAME}" # Path to the OpenAPI lint rules OPENAPI_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${OPENAPI_FILE_NAME}" # Path to the OpenAPI lint rules
# PHP Vars
PHPSTAN_FILE_NAME='phpstan.neon' # Name of the file
PHPSTAN_LINTER_RULES="${GITHUB_WORKSPACE}/${PHPSTAN_FILE_NAME}" # Path to the PHPStan lint rules in the repository
if [ ! -f "$PHPSTAN_LINTER_RULES" ]; then
PHPSTAN_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${PHPSTAN_FILE_NAME}" # Path to the PHPStan lint rules
fi
# Powershell Vars # Powershell Vars
POWERSHELL_FILE_NAME='.powershell-psscriptanalyzer.psd1' # Name of the file POWERSHELL_FILE_NAME='.powershell-psscriptanalyzer.psd1' # Name of the file
POWERSHELL_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${POWERSHELL_FILE_NAME}" # Path to the Powershell lint rules POWERSHELL_LINTER_RULES="${DEFAULT_RULES_LOCATION}/${POWERSHELL_FILE_NAME}" # Path to the Powershell lint rules
@ -110,7 +116,7 @@ LINTER_ARRAY=('ansible-lint' 'arm-ttk' 'asl-validator' 'cfn-lint' 'clj-kondo' 'c
############################# #############################
LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'CLOUDFORMATION' 'CLOJURE' 'COFFEESCRIPT' 'CSS' LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'CLOUDFORMATION' 'CLOJURE' 'COFFEESCRIPT' 'CSS'
'DART' 'DOCKER' 'ENV' 'GO' 'GROOVY' 'HTML' 'JAVASCRIPT_ES' 'JAVASCRIPT_STANDARD' 'DART' 'DOCKER' 'ENV' 'GO' 'GROOVY' 'HTML' 'JAVASCRIPT_ES' 'JAVASCRIPT_STANDARD'
'JSON' 'JSX' 'KOTLIN' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' 'PHP' 'POWERSHELL' 'JSON' 'JSX' 'KOTLIN' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' 'PHP' 'PHP_PHPSTAN' 'POWERSHELL'
'PROTOBUF' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'RAKU' 'RUBY' 'STATES' 'TERRAFORM' 'PROTOBUF' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'RAKU' 'RUBY' 'STATES' 'TERRAFORM'
'TERRAFORM_TERRASCAN' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YML') 'TERRAFORM_TERRASCAN' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' 'XML' 'YML')
@ -159,6 +165,7 @@ VALIDATE_MARKDOWN="${VALIDATE_MD:-}" # Boolean
VALIDATE_OPENAPI="${VALIDATE_OPENAPI}" # Boolean to validate language VALIDATE_OPENAPI="${VALIDATE_OPENAPI}" # Boolean to validate language
VALIDATE_PERL="${VALIDATE_PERL}" # Boolean to validate language VALIDATE_PERL="${VALIDATE_PERL}" # Boolean to validate language
VALIDATE_PHP="${VALIDATE_PHP}" # Boolean to validate language VALIDATE_PHP="${VALIDATE_PHP}" # Boolean to validate language
VALIDATE_PHP_PHPSTAN="${VALIDATE_PHP_PHPSTAN}" # Boolean to validate language
VALIDATE_POWERSHELL="${VALIDATE_POWERSHELL}" # Boolean to validate language VALIDATE_POWERSHELL="${VALIDATE_POWERSHELL}" # Boolean to validate language
VALIDATE_PYTHON_PYLINT="${VALIDATE_PYTHON:-$VALIDATE_PYTHON_PYLINT}" # Boolean to validate language VALIDATE_PYTHON_PYLINT="${VALIDATE_PYTHON:-$VALIDATE_PYTHON_PYLINT}" # Boolean to validate language
VALIDATE_PYTHON_FLAKE8="${VALIDATE_PYTHON_FLAKE8}" # Boolean to validate language VALIDATE_PYTHON_FLAKE8="${VALIDATE_PYTHON_FLAKE8}" # Boolean to validate language
@ -237,6 +244,7 @@ FILE_ARRAY_MARKDOWN=() # Array of files to check
FILE_ARRAY_OPENAPI=() # Array of files to check FILE_ARRAY_OPENAPI=() # Array of files to check
FILE_ARRAY_PERL=() # Array of files to check FILE_ARRAY_PERL=() # Array of files to check
FILE_ARRAY_PHP=() # Array of files to check FILE_ARRAY_PHP=() # Array of files to check
FILE_ARRAY_PHP_PHPSTAN=() # Array of files to check
FILE_ARRAY_POWERSHELL=() # Array of files to check FILE_ARRAY_POWERSHELL=() # Array of files to check
FILE_ARRAY_PROTOBUF=() # Array of files to check FILE_ARRAY_PROTOBUF=() # Array of files to check
FILE_ARRAY_PYTHON_PYLINT=() # Array of files to check FILE_ARRAY_PYTHON_PYLINT=() # Array of files to check
@ -300,6 +308,8 @@ ERRORS_FOUND_PERL=0 # Count of errors found
export ERRORS_FOUND_PERL # Workaround SC2034 export ERRORS_FOUND_PERL # Workaround SC2034
ERRORS_FOUND_PHP=0 # Count of errors found ERRORS_FOUND_PHP=0 # Count of errors found
export ERRORS_FOUND_PHP # Workaround SC2034 export ERRORS_FOUND_PHP # Workaround SC2034
ERRORS_FOUND_PHP_PHPSTAN=0 # Count of errors found
export ERRORS_FOUND_PHP_PHPSTAN # Workaround SC2034
ERRORS_FOUND_POWERSHELL=0 # Count of errors found ERRORS_FOUND_POWERSHELL=0 # Count of errors found
export ERRORS_FOUND_POWERSHELL # Workaround SC2034 export ERRORS_FOUND_POWERSHELL # Workaround SC2034
ERRORS_FOUND_PROTOBUF=0 # Count of errors found ERRORS_FOUND_PROTOBUF=0 # Count of errors found
@ -1487,6 +1497,17 @@ if [ "${VALIDATE_PHP}" == "true" ]; then
LintCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "${FILE_ARRAY_PHP[@]}" LintCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "${FILE_ARRAY_PHP[@]}"
fi fi
###################
# PHPStan LINTING #
###################
if [ "${VALIDATE_PHP_PHPSTAN}" == "true" ]; then
#######################
# Lint the PHP files #
#######################
# LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY"
LintCodebase "PHP_PHPSTAN" "phpstan" "phpstan analyse --no-progress --no-ansi -c ${PHPSTAN_LINTER_RULES}" ".*\.\(php\)\$" "${FILE_ARRAY_PHP_PHPSTAN[@]}"
fi
###################### ######################
# POWERSHELL LINTING # # POWERSHELL LINTING #
###################### ######################

View file

@ -599,6 +599,7 @@ function RunTestCases() {
TestCodebase "MARKDOWN" "markdownlint" "markdownlint -c ${MARKDOWN_LINTER_RULES}" ".*\.\(md\)\$" "markdown" TestCodebase "MARKDOWN" "markdownlint" "markdownlint -c ${MARKDOWN_LINTER_RULES}" ".*\.\(md\)\$" "markdown"
TestCodebase "PERL" "perl" "perl -Mstrict -cw" ".*\.\(pl\)\$" "perl" TestCodebase "PERL" "perl" "perl -Mstrict -cw" ".*\.\(pl\)\$" "perl"
TestCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "php" TestCodebase "PHP" "php" "php -l" ".*\.\(php\)\$" "php"
TestCodebase "PHP_PHPSTAN" "phpstan" "phpstan analyse --no-progress --no-ansi -c ${PHPSTAN_LINTER_RULES}" ".*\.\(php\)\$" "php"
TestCodebase "OPENAPI" "spectral" "spectral lint -r ${OPENAPI_LINTER_RULES}" ".*\.\(ymlopenapi\|jsonopenapi\)\$" "openapi" TestCodebase "OPENAPI" "spectral" "spectral lint -r ${OPENAPI_LINTER_RULES}" ".*\.\(ymlopenapi\|jsonopenapi\)\$" "openapi"
TestCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings ${POWERSHELL_LINTER_RULES} -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "powershell" TestCodebase "POWERSHELL" "pwsh" "Invoke-ScriptAnalyzer -EnableExit -Settings ${POWERSHELL_LINTER_RULES} -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "powershell"
TestCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "protobuf" TestCodebase "PROTOBUF" "protolint" "protolint lint --config_path ${PROTOBUF_LINTER_RULES}" ".*\.\(proto\)\$" "protobuf"