From 85c412b5c03ebcd15a911858edec7ebde10491e9 Mon Sep 17 00:00:00 2001 From: lipemat Date: Sat, 14 Oct 2023 10:03:38 -0500 Subject: [PATCH 1/3] Only use multi version support in pre-commit if the environment supports Make the hook portable to other types of environments. --- dev/git-hooks/pre-commit | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dev/git-hooks/pre-commit b/dev/git-hooks/pre-commit index d8a38e4..6892447 100644 --- a/dev/git-hooks/pre-commit +++ b/dev/git-hooks/pre-commit @@ -18,9 +18,15 @@ RED="$(tput setaf 1)" BLUE="$(tput setaf 6)" RESET_COLOR="$(tput sgr0)" -PHP='php 7.2' +# Point to specific version of PHP if supported via environmental variables. +if [[ "true" == "$PHP_MULTI_VERSION_SUPPORT" ]]; then + PHP='php 7.2' + PHPUNIT='phpunit 7.2' +else + PHP='php' + PHPUNIT='phpunit' +fi PHPCS='phpcs' -PHPUNIT='phpunit 7.2' PHPSTAN='phpstan' PROJECT=$(${PHP} -r "echo dirname(realpath('$0'), 3);") From c0527c41423291ef207524c2842d45225ea1560d Mon Sep 17 00:00:00 2001 From: lipemat Date: Sat, 14 Oct 2023 10:05:39 -0500 Subject: [PATCH 2/3] Remove getTerm and getTermBy services The types are now included in the wordpress-stubs as of version 6.3.2 --- extension.neon | 6 -- ...rmByDynamicFunctionReturnTypeExtension.php | 90 ------------------- ...TermDynamicFunctionReturnTypeExtension.php | 89 ------------------ 3 files changed, 185 deletions(-) delete mode 100644 src/Services/GetTermByDynamicFunctionReturnTypeExtension.php delete mode 100644 src/Services/GetTermDynamicFunctionReturnTypeExtension.php diff --git a/extension.neon b/extension.neon index 3ada255..31c4818 100644 --- a/extension.neon +++ b/extension.neon @@ -2,12 +2,6 @@ services: - class: Lipe\Lib\Phpstan\Services\DynamicFunctionReturnType\GetApprovedComments tags: - phpstan.broker.dynamicFunctionReturnTypeExtension - - class: Lipe\Lib\Phpstan\Services\GetTermDynamicFunctionReturnTypeExtension - tags: - - phpstan.broker.dynamicFunctionReturnTypeExtension - - class: Lipe\Lib\Phpstan\Services\GetTermByDynamicFunctionReturnTypeExtension - tags: - - phpstan.broker.dynamicFunctionReturnTypeExtension - class: Lipe\Lib\Phpstan\Services\DbGetDynamicFunctionReturnTypeExtension tags: - phpstan.broker.dynamicMethodReturnTypeExtension diff --git a/src/Services/GetTermByDynamicFunctionReturnTypeExtension.php b/src/Services/GetTermByDynamicFunctionReturnTypeExtension.php deleted file mode 100644 index 9abdc11..0000000 --- a/src/Services/GetTermByDynamicFunctionReturnTypeExtension.php +++ /dev/null @@ -1,90 +0,0 @@ -getName(), static::$supported, true ); - } - - - /** - * - Return `array` if `$output = 'ARRAY_N`. - * - Return `array` if default or `$output = 'ARRAY_A`. - * - Return `WP_Term` if default or `$output` is 'OBJECT'; - * - * @link https://developer.wordpress.org/reference/functions/get_term_by/#parameters - */ - public function getTypeFromFunctionCall( FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope ): ?Type { - $args = $functionCall->getArgs(); - - if ( count( $args ) < 4 ) { - return static::termsType(); - } - - $value = TypeCombinator::union( - new StringType(), - new IntegerType() - ); - $argumentType = $scope->getType( $args[3]->value ); - if ( 1 === count( $argumentType->getConstantStrings() ) ) { - $returnType = []; - switch ( $argumentType->getConstantStrings()[0]->getValue() ) { - case 'ARRAY_A': - $returnType[] = new ArrayType( new StringType(), $value ); - break; - case 'ARRAY_N': - $returnType[] = new ArrayType( new IntegerType(), $value ); - break; - default: - return static::termsType(); - } - - $returnType[] = new ObjectType( 'WP_Error' ); - $returnType[] = new ConstantBooleanType( \false ); - return TypeCombinator::union( ...$returnType ); - } - - return static::termsType(); - } -} diff --git a/src/Services/GetTermDynamicFunctionReturnTypeExtension.php b/src/Services/GetTermDynamicFunctionReturnTypeExtension.php deleted file mode 100644 index baba608..0000000 --- a/src/Services/GetTermDynamicFunctionReturnTypeExtension.php +++ /dev/null @@ -1,89 +0,0 @@ -getName(), static::$supported, true ); - } - - - /** - * - Return `array` if `$output = 'ARRAY_N`. - * - Return `array` if default or `$output = 'ARRAY_A`. - * - Return `WP_Term` if default or `$output` is 'OBJECT'; - * - * @link https://developer.wordpress.org/reference/functions/get_term/#parameters - */ - public function getTypeFromFunctionCall( FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope ): ?Type { - $args = $functionCall->getArgs(); - - if ( count( $args ) < 3 ) { - return static::termsType(); - } - - $value = TypeCombinator::union( - new StringType(), - new IntegerType() - ); - $argumentType = $scope->getType( $args[2]->value ); - if ( 1 === count( $argumentType->getConstantStrings() ) ) { - $returnType = []; - switch ( $argumentType->getConstantStrings()[0]->getValue() ) { - case 'ARRAY_A': - $returnType[] = new ArrayType( new StringType(), $value ); - break; - case 'ARRAY_N': - $returnType[] = new ArrayType( new IntegerType(), $value ); - break; - default: - return static::termsType(); - } - - $returnType[] = new ObjectType( 'WP_Error' ); - $returnType[] = new NullType(); - return TypeCombinator::union( ...$returnType ); - } - - return static::termsType(); - } -} From bafc8b79599f25678b627a3ddc4e9a16d1243b43 Mon Sep 17 00:00:00 2001 From: lipemat Date: Sat, 14 Oct 2023 10:21:13 -0500 Subject: [PATCH 3/3] Update pre-commit to version 5.3.0 - Run on every commit instead of just ones with PHP lintable files. --- dev/git-hooks/pre-commit | 116 +++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/dev/git-hooks/pre-commit b/dev/git-hooks/pre-commit index 6892447..db313b0 100644 --- a/dev/git-hooks/pre-commit +++ b/dev/git-hooks/pre-commit @@ -7,8 +7,12 @@ # # @uses Global PHP version as different projects have different requirements. # -# @version 4.5.1 +# @version 5.3.0 # + +PHP_VERSION="7.2" +PHPUNIT_DIR="dev/phpunit" + ##################################################################################### GREEN="$(tput setaf 2)" @@ -20,71 +24,67 @@ RESET_COLOR="$(tput sgr0)" # Point to specific version of PHP if supported via environmental variables. if [[ "true" == "$PHP_MULTI_VERSION_SUPPORT" ]]; then - PHP='php 7.2' - PHPUNIT='phpunit 7.2' + PHP="php ${PHP_VERSION}" + PHPCS='phpcs' + PHPSTAN='phpstan' + PHPUNIT="phpunit ${PHP_VERSION}" else PHP='php' + PHPCS='phpcs' + PHPSTAN='phpstan' PHPUNIT='phpunit' fi -PHPCS='phpcs' -PHPSTAN='phpstan' - PROJECT=$(${PHP} -r "echo dirname(realpath('$0'), 3);") ## Fix windows paths PROJECT=${PROJECT//\\//} PHP_FILES=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD | grep \\.php) if [[ ! "$PHP_FILES" ]]; then - echo "${YELLOW}[pre-commit]${BLUE} No PHP Files Changed ${RESET_COLOR}" - exit 0 + echo "${YELLOW}[pre-commit]${BLUE} No Lintable PHP Files Changed ${RESET_COLOR}" fi function exit_reset_colors() { - echo ${RESET_COLOR} - exit 1 + echo ${RESET_COLOR} + exit 1 } function php_unit() { - if [[ "$PHP_FILES" ]]; then - if [[ ! -f ${PROJECT}/dev/phpunit/phpunit.xml ]] && [[ ! -f ${PROJECT}/dev/phpunit/phpunit.xml.dist ]]; then - echo "${YELLOW}[pre-commit]${RED} ${PROJECT}/dev/phpunit/phpunit.xml or ${PROJECT}/dev/phpunit/phpunit.xml.dist not found!" - exit_reset_colors - fi - echo "${YELLOW}[pre-commit]${BLUE} Running PHP Unit... ${WHITE}" - cd "${PROJECT}/dev/phpunit" || exit - OUTPUT=$(${PHPUNIT}) - if [[ $? != 0 ]]; then - echo - echo "${BLUE}PHP Unit Failed! Fix the error before commit!" - echo "${RED}$OUTPUT" - exit_reset_colors - fi - echo "${YELLOW}[pre-commit]${GREEN} PHP Unit Tests Passed!${WHITE}" + if [[ ! -f ${PROJECT}/${PHPUNIT_DIR}/phpunit.xml ]] && [[ ! -f ${PROJECT}/${PHPUNIT_DIR}/phpunit.xml.dist ]]; then + echo "${YELLOW}[pre-commit]${RED} ${PROJECT}/${PHPUNIT_DIR}/phpunit.xml or ${PROJECT}/${PHPUNIT_DIR}/phpunit.xml.dist not found!" + exit_reset_colors fi + echo "${YELLOW}[pre-commit]${BLUE} Running PHP Unit... ${WHITE}" + cd "${PROJECT}/${PHPUNIT_DIR}" || exit + OUTPUT=$(${PHPUNIT}) + if [[ $? != 0 ]]; then + echo + echo "${BLUE}PHP Unit Failed! Fix the error before commit!" + echo "${RED}$OUTPUT" + exit_reset_colors + fi + echo "${YELLOW}[pre-commit]${GREEN} PHP Unit Tests Passed!${RESET_COLOR}" } php_unit & function php_lint() { - if [[ "$PHP_FILES" ]]; then - # Run php lint. - echo "${YELLOW}[pre-commit]${BLUE} Checking ${1} Lint... ${WHITE}" - for FILE in ${PHP_FILES}; do - OUTPUT=$(${1} -l -d display_errors=0 ${PROJECT}/${FILE}) - if [[ $? != 0 ]]; then - echo - echo "${BLUE}${1} Lint Failed. Fix the error before commit." - echo "${RED}$OUTPUT" - exit_reset_colors - fi - done - echo "${YELLOW}[pre-commit]${GREEN} ${1} Lint Passed!${WHITE}" - fi + if [[ "$PHP_FILES" ]]; then + echo "${YELLOW}[pre-commit]${BLUE} Checking PHP Lint... ${WHITE}" + for FILE in ${PHP_FILES}; do + OUTPUT=$(${PHP} -l -d display_errors=0 ${PROJECT}/${FILE}) + if [[ $? != 0 ]]; then + echo + echo "${BLUE}PHP Lint Failed. Fix the error before commit." + echo "${RED}$OUTPUT" + exit_reset_colors + fi + done + echo "${YELLOW}[pre-commit]${GREEN} PHP Lint Passed!${RESET_COLOR}" + fi } - -php_lint 'php' & +php_lint & function php_code_sniffer() { - if [[ "$PHP_FILES" != "" ]]; then + if [[ "$PHP_FILES" || "$JS_FILES" ]]; then if [[ ! -f ${PROJECT}/phpcs.xml ]] && [[ ! -f ${PROJECT}/phpcs.xml.dist ]]; then echo "${YELLOW}[pre-commit]${RED} ${PROJECT}/phpcs.xml or ${PROJECT}/phpcs.xml.dist not found!" exit_reset_colors @@ -93,7 +93,7 @@ function php_code_sniffer() { FILES="$FILES ${PROJECT}/${FILE}" done echo "${YELLOW}[pre-commit]${BLUE} Running PHPCS... ${WHITE}" - OUTPUT=$(${PHPCS} --encoding=utf-8 -s -p ${FILES}) + OUTPUT=$(${PHPCS} --encoding=utf-8 -s -p) if [[ $? != 0 ]]; then echo echo "${BLUE}PHPCS failed. Fix the error before commit!" @@ -101,27 +101,25 @@ function php_code_sniffer() { echo echo "${YELLOW}For automatic fixes run" echo - echo "${WHITE}phpcbf $FILES" + echo "${WHITE}phpcbf${FILES}" echo exit_reset_colors fi - echo "${YELLOW}[pre-commit]${GREEN} PHPCS Passed!${WHITE}" + echo "${YELLOW}[pre-commit]${GREEN} PHPCS Passed!${RESET_COLOR}" fi } php_code_sniffer & function php_stan() { - if [[ "$PHP_FILES" ]]; then - echo "${YELLOW}[pre-commit]${BLUE} Running PHP Stan... ${WHITE}" - OUTPUT=$(${PHPSTAN} analyse --no-progress --memory-limit=2G) - if [[ $? != 0 ]]; then - echo - echo "${BLUE}PHP Stan Failed. Fix the error before commit." - echo "${RED}$OUTPUT" - exit_reset_colors - fi - echo "${YELLOW}[pre-commit]${GREEN} PHP Stan Passed!${WHITE}" - fi + echo "${YELLOW}[pre-commit]${BLUE} Running PHP Stan... ${WHITE}" + OUTPUT=$(${PHPSTAN} analyse --no-progress --memory-limit=2G) + if [[ $? != 0 ]]; then + echo + echo "${BLUE}PHP Stan Failed. Fix the error before commit." + echo "${RED}$OUTPUT" + exit_reset_colors + fi + echo "${YELLOW}[pre-commit]${GREEN} PHP Stan Passed!${RESET_COLOR}" } php_stan & @@ -129,14 +127,14 @@ php_stan & # If it sent exit code 1, it failed and the result from `wait` will be false. FAIL=0 for job in $(jobs -p); do - wait "$job" || ((FAIL += 1)) + wait "$job" || (( FAIL+=1 )) done echo "${RESET_COLOR}" # If any of the background tasks failed, we exit 1 if [[ $FAIL -ne 0 ]]; then - exit 1 + exit 1 else - exit 0 + exit 0 fi