From f5b6c8818415321b5c8adf8045b8013c68282744 Mon Sep 17 00:00:00 2001 From: Klaus Purer <klaus.purer@protonmail.ch> Date: Sun, 5 Jan 2020 21:14:17 +0100 Subject: [PATCH] tests(phpstan): Add PHPStan to detect problems in sniff code (#3104477) --- .travis.yml | 4 +++ .../Arrays/DisallowLongArraySyntaxSniff.php | 2 +- .../Sniffs/Commenting/FileCommentSniff.php | 2 -- .../Sniffs/Semantics/FunctionAliasSniff.php | 18 ------------ composer.json | 2 +- phpstan.neon | 28 +++++++++++++++++++ 6 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 phpstan.neon diff --git a/.travis.yml b/.travis.yml index 0cff50a3..3f29a999 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ cache: jobs: fast_finish: true include: + # Support PHP 7.0 because that is the minimum requirement of Drupal 8 core. - php: 7.0 dist: xenial - php: 7.1 @@ -20,6 +21,7 @@ jobs: dist: bionic - php: 7.4 dist: bionic + env: PHPSTAN=1 before_install: # Speed up build time by disabling Xdebug when its not needed. @@ -40,3 +42,5 @@ script: # Ensure that the DrupalPractice standard can be invoked standalone and the # auto-loading of abstract classes works. - ./vendor/bin/phpcs -p --standard=coder_sniffer/DrupalPractice coder_sniffer/DrupalPractice/Test/good/ --ignore=coder_sniffer/DrupalPractice/Test/good/GoodUnitTest.php + # We cannot add PHPStan to composer.json because it cannot be installed in PHP 7.0. + - if [[ $PHPSTAN == "1" ]]; then composer require --dev phpstan/phpstan && ./vendor/bin/phpstan analyse; fi diff --git a/coder_sniffer/Drupal/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php b/coder_sniffer/Drupal/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php index 6fe8f2af..356e3628 100644 --- a/coder_sniffer/Drupal/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php @@ -41,7 +41,7 @@ class DisallowLongArraySyntaxSniff extends GenericDisallowLongArraySyntaxSniff return ($phpcsFile->numTokens + 1); } - return parent::process($phpcsFile, $stackPtr); + parent::process($phpcsFile, $stackPtr); }//end process() diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/FileCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/FileCommentSniff.php index ecd340b7..f2196eb0 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/FileCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/FileCommentSniff.php @@ -64,8 +64,6 @@ class FileCommentSniff implements Sniff */ public function process(File $phpcsFile, $stackPtr) { - $this->currentFile = $phpcsFile; - $tokens = $phpcsFile->getTokens(); $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); diff --git a/coder_sniffer/Drupal/Sniffs/Semantics/FunctionAliasSniff.php b/coder_sniffer/Drupal/Sniffs/Semantics/FunctionAliasSniff.php index 5f44eb5b..3fa1f3c1 100644 --- a/coder_sniffer/Drupal/Sniffs/Semantics/FunctionAliasSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Semantics/FunctionAliasSniff.php @@ -94,42 +94,25 @@ class FunctionAliasSniff extends FunctionCall 'msql_selectdb' => 'msql_select_db', 'msql_tablename' => 'msql_result', 'mssql_affected_rows' => 'sybase_affected_rows', - 'mssql_affected_rows' => 'sybase_affected_rows', - 'mssql_close' => 'sybase_close', 'mssql_close' => 'sybase_close', 'mssql_connect' => 'sybase_connect', - 'mssql_connect' => 'sybase_connect', 'mssql_data_seek' => 'sybase_data_seek', - 'mssql_data_seek' => 'sybase_data_seek', - 'mssql_fetch_array' => 'sybase_fetch_array', 'mssql_fetch_array' => 'sybase_fetch_array', 'mssql_fetch_field' => 'sybase_fetch_field', - 'mssql_fetch_field' => 'sybase_fetch_field', - 'mssql_fetch_object' => 'sybase_fetch_object', 'mssql_fetch_object' => 'sybase_fetch_object', 'mssql_fetch_row' => 'sybase_fetch_row', - 'mssql_fetch_row' => 'sybase_fetch_row', - 'mssql_field_seek' => 'sybase_field_seek', 'mssql_field_seek' => 'sybase_field_seek', 'mssql_free_result' => 'sybase_free_result', - 'mssql_free_result' => 'sybase_free_result', - 'mssql_get_last_message' => 'sybase_get_last_message', 'mssql_get_last_message' => 'sybase_get_last_message', 'mssql_min_client_severity' => 'sybase_min_client_severity', 'mssql_min_error_severity' => 'sybase_min_error_severity', 'mssql_min_message_severity' => 'sybase_min_message_severity', 'mssql_min_server_severity' => 'sybase_min_server_severity', 'mssql_num_fields' => 'sybase_num_fields', - 'mssql_num_fields' => 'sybase_num_fields', - 'mssql_num_rows' => 'sybase_num_rows', 'mssql_num_rows' => 'sybase_num_rows', 'mssql_pconnect' => 'sybase_pconnect', - 'mssql_pconnect' => 'sybase_pconnect', - 'mssql_query' => 'sybase_query', 'mssql_query' => 'sybase_query', 'mssql_result' => 'sybase_result', - 'mssql_result' => 'sybase_result', - 'mssql_select_db' => 'sybase_select_db', 'mssql_select_db' => 'sybase_select_db', 'mysql' => 'mysql_db_query', 'mysql_createdb' => 'mysql_create_db', @@ -153,7 +136,6 @@ class FunctionAliasSniff extends FunctionCall 'oci8assign' => 'ocicollassign', 'oci8assignelem' => 'ocicollassignelem', 'oci8close' => 'ocicloselob', - 'oci8free' => 'ocifreecoll', 'oci8free' => 'ocifreedesc', 'oci8getelem' => 'ocicollgetelem', 'oci8load' => 'ociloadlob', diff --git a/composer.json b/composer.json index 53f3f232..30e33a06 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "symfony/yaml": ">=2.0.5" }, "autoload": { - "psr-0": { + "psr-4": { "Drupal\\": "coder_sniffer/Drupal/", "DrupalPractice\\": "coder_sniffer/DrupalPractice/" } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..bf7f8708 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,28 @@ +parameters: + level: 0 + paths: + - coder_sniffer/Drupal/Sniffs + autoload_files: + - coder_sniffer/Drupal/Test/phpunit-bootstrap.php + ignoreErrors: + # Already fixed in PHP_CodeSniffer, remove once 3.5.4 is released. + - + count: 3 + message: '~^Cannot unset offset~' + path: %currentWorkingDirectory%/coder_sniffer/Drupal/Sniffs/Formatting/MultipleStatementAlignmentSniff.php + # PHPStan does not support variable variables, see https://github.com/phpstan/phpstan/issues/2810 + - + count: 3 + message: '~^Undefined variable: \$value[123]$~' + path: %currentWorkingDirectory%/coder_sniffer/Drupal/Sniffs/InfoFiles/ClassFilesSniff.php + - + count: 3 + message: '~^Undefined variable: \$value[123]$~' + path: %currentWorkingDirectory%/coder_sniffer/Drupal/Sniffs/InfoFiles/DuplicateEntrySniff.php + # We don't want to introduce abstract methods for the 2 calls because + # the class is used in different ways where those methods are not + # always necessary for implementors. + - + count: 2 + message: '~^Call to an undefined method Drupal\\Sniffs\\Semantics\\FunctionCall::(registerFunctionNames|processFunctionCall)\(\).$~' + path: %currentWorkingDirectory%/coder_sniffer/Drupal/Sniffs/Semantics/FunctionCall.php -- GitLab