Skip to content

Feature/ghactions tweaks#1

Closed
jrfnl wants to merge 4 commits into
masterfrom
feature/ghactions-tweaks
Closed

Feature/ghactions tweaks#1
jrfnl wants to merge 4 commits into
masterfrom
feature/ghactions-tweaks

Conversation

@jrfnl

@jrfnl jrfnl commented Feb 28, 2021

Copy link
Copy Markdown
Owner

Testing CS2PR

* No need to install via Composer, the `setup-php` action can make PHPStan globally available.
jrfnl added a commit that referenced this pull request Feb 28, 2021
The `Util\Standards::getInstalledStandardPath()` method returns either a string or `null`, however the `strpos()` method which is used in both the `Util\Common::isPharFile()` method, as well as in the follow-on condition, only accepts a `string` as `$haystack`.

As of PHP 8.1, this will generated a deprecation notice `strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated`.

Discovered by testing an external standard against PHPCS `master` on PHP 8.1.

Fixed now.
jrfnl added a commit that referenced this pull request Feb 28, 2021
The `Util\Standards::getInstalledStandardPath()` method returns either a string or `null`, however the `strpos()` method which is used in both the `Util\Common::isPharFile()` method, as well as in the follow-on condition, only accepts a `string` as `$haystack`.

As of PHP 8.1, this will generate a deprecation notice `strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated`.

Discovered while testing an external standard against PHPCS `master` on PHP 8.1.

Fixed now.
There is no need to remove the config file and have all settings on the command-line as PHPUnit offers a `--no-configuration` option to ignore the config file.

Enabling that simplifies the action steps a little and isolates the customization for PHP 8.1 to PHP 8.1.
@jrfnl jrfnl force-pushed the feature/ghactions-tweaks branch from 72b9970 to cc678d1 Compare February 28, 2021 05:07
The cs2pr tool will allow to display the results from an action run in checkstyle format in-line in the PR code view, which should improve usability of the workflow results.

As the CS run is run in every test run in the matrix, passing the results on to CS2PR would result in violations being displayed multiple times, so using conditions to only pass the CS violations on to the PR for one of the runs.

Ref: https://github.com/staabm/annotate-pull-request-from-checkstyle
@jrfnl jrfnl force-pushed the feature/ghactions-tweaks branch from 4e9a01d to a7b2b95 Compare February 28, 2021 05:24
@jrfnl jrfnl closed this Feb 28, 2021
jrfnl added a commit that referenced this pull request Mar 5, 2021
The `Util\Standards::getInstalledStandardPath()` method returns either a string or `null`, however the `strpos()` method which is used in both the `Util\Common::isPharFile()` method, as well as in the follow-on condition, only accepts a `string` as `$haystack`.

As of PHP 8.1, this will generate a deprecation notice `strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated`.

Discovered while testing an external standard against PHPCS `master` on PHP 8.1.

Fixed now.
jrfnl added a commit that referenced this pull request Jun 21, 2021
On PHP 8.1, passing `null` to `strtolower()` generates a `strtolower(): Passing null to parameter #1 ($string) of type string is deprecated` notice.

The `File::getDeclarationName()` will return `null` for anonymous classes, so we need an extra safeguard here.

This fix is covered by the existing unit tests and was exposed when running the tests on PHP 8.1 with `error_reporting` set to `E_ALL`.
jrfnl added a commit that referenced this pull request Sep 25, 2021
On PHP 8.1, passing `null` to `strtolower()` generates a `strtolower(): Passing null to parameter #1 ($string) of type string is deprecated` notice.

The `File::getDeclarationName()` will return `null` for anonymous classes, so we need an extra safeguard here.

This fix is covered by the existing unit tests and was exposed when running the tests on PHP 8.1 with `error_reporting` set to `E_ALL`.
jrfnl added a commit that referenced this pull request Jul 29, 2024
…-home

Update for Composer name + docs for new repo home
jrfnl added a commit that referenced this pull request Jul 29, 2024
jrfnl added a commit that referenced this pull request Nov 6, 2024
This adds another set of dedicated tests to safeguard how XML docs which don't follow the specification are handled.

This initial set of tests for this documents the current behaviour [*]. This behaviour may not always be the desired behaviour, in which case, this will be fixed in follow-up commits.

To get these tests up and running, the following fatal errors needed to be fixed:

* Fatal error when a code comparison only contains a single code element:
    ```
    Fatal error: Uncaught Error: Call to a member function getAttribute() on null in path/to/PHP_CodeSniffer/src/Generators/Markdown.php:253
    Stack trace:
    #0 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(139): PHP_CodeSniffer\Generators\Markdown->getFormattedCodeComparisonBlock(Object(DOMElement))
    #1 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(39): PHP_CodeSniffer\Generators\Markdown->processSniff(Object(DOMElement))
    squizlabs#2 path/to/PHP_CodeSniffer/src/Runner.php(99): PHP_CodeSniffer\Generators\Markdown->generate()
    squizlabs#3 path/to/PHP_CodeSniffer/bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
    squizlabs#4 {main}
      thrown in path/to/PHP_CodeSniffer/src/Generators/Markdown.php on line 253
    ```

* Fatal error when a code element contains no textual content:
    ```
    Fatal error: Uncaught Error: Call to a member function getAttribute() on null in path/to/PHP_CodeSniffer/src/Generators/Markdown.php:246
    Stack trace:
    #0 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(139): PHP_CodeSniffer\Generators\Markdown->getFormattedCodeComparisonBlock(Object(DOMElement))
    #1 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(39): PHP_CodeSniffer\Generators\Markdown->processSniff(Object(DOMElement))
    squizlabs#2 path/to/PHP_CodeSniffer/src/Runner.php(99): PHP_CodeSniffer\Generators\Markdown->generate()
    squizlabs#3 path/to/PHP_CodeSniffer/bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
    squizlabs#4 {main}
      thrown in path/to/PHP_CodeSniffer/src/Generators/Markdown.php on line 246
    ```

Both of these fatals are fixed by adding defensive coding validating that there are (at least) two code blocks to the `getFormattedCodeComparisonBlock()` methods for all three generator classes.
jrfnl added a commit that referenced this pull request Feb 19, 2025
This adds another set of dedicated tests to safeguard how XML docs which don't follow the specification are handled.

This initial set of tests for this documents the current behaviour [*]. This behaviour may not always be the desired behaviour, in which case, this will be fixed in follow-up commits.

To get these tests up and running, the following fatal errors needed to be fixed:

* Fatal error when a code comparison only contains a single code element:
    ```
    Fatal error: Uncaught Error: Call to a member function getAttribute() on null in path/to/PHP_CodeSniffer/src/Generators/Markdown.php:253
    Stack trace:
    #0 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(139): PHP_CodeSniffer\Generators\Markdown->getFormattedCodeComparisonBlock(Object(DOMElement))
    #1 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(39): PHP_CodeSniffer\Generators\Markdown->processSniff(Object(DOMElement))
    squizlabs#2 path/to/PHP_CodeSniffer/src/Runner.php(99): PHP_CodeSniffer\Generators\Markdown->generate()
    squizlabs#3 path/to/PHP_CodeSniffer/bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
    squizlabs#4 {main}
      thrown in path/to/PHP_CodeSniffer/src/Generators/Markdown.php on line 253
    ```

* Fatal error when a code element contains no textual content:
    ```
    Fatal error: Uncaught Error: Call to a member function getAttribute() on null in path/to/PHP_CodeSniffer/src/Generators/Markdown.php:246
    Stack trace:
    #0 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(139): PHP_CodeSniffer\Generators\Markdown->getFormattedCodeComparisonBlock(Object(DOMElement))
    #1 path/to/PHP_CodeSniffer/src/Generators/Markdown.php(39): PHP_CodeSniffer\Generators\Markdown->processSniff(Object(DOMElement))
    squizlabs#2 path/to/PHP_CodeSniffer/src/Runner.php(99): PHP_CodeSniffer\Generators\Markdown->generate()
    squizlabs#3 path/to/PHP_CodeSniffer/bin/phpcs(14): PHP_CodeSniffer\Runner->runPHPCS()
    squizlabs#4 {main}
      thrown in path/to/PHP_CodeSniffer/src/Generators/Markdown.php on line 246
    ```

Both of these fatals are fixed by adding defensive coding validating that there are (at least) two code blocks to the `getFormattedCodeComparisonBlock()` methods for all three generator classes.
jrfnl added a commit that referenced this pull request Feb 19, 2025
This sniff could throw the following fatal errors when it encountered parse errors/during live coding:

```
Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in path/to/PHPCS/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php on line 83 in path/to/PHPCS/src/Runner.php on line 624

Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in path/to/PHPCS/src/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php on line 167 in path/to/PHPCS/src/Runner.php on line 624
```

Fixed now by adding more defensive coding.

Includes test safeguarding the fix.
jrfnl added a commit that referenced this pull request Apr 29, 2025
…report

Okay, so this is an awkward one.

If both the `Code` + the `Diff` report were requested + caching was turned on + the _order_ of the requested reports was `Code,Diff` (i.e. first the code report), the following fatal error which would occur:
```
Fatal error: Uncaught Error: Call to a member function getFixableCount() on null in path/to/PHP_CodeSniffer/src/Fixer.php:144
Stack trace:
#0 path/to/PHP_CodeSniffer/src/Reports/Diff.php(73): PHP_CodeSniffer\Fixer->fixFile()
#1 path/to/PHP_CodeSniffer/src/Reporter.php(285): PHP_CodeSniffer\Reports\Diff->generateFileReport(Array, Object(PHP_CodeSniffer\Files\LocalFile), true, 150)
squizlabs#2 path/to/PHP_CodeSniffer/src/Runner.php(659): PHP_CodeSniffer\Reporter->cacheFileReport(Object(PHP_CodeSniffer\Files\LocalFile))
squizlabs#3 path/to/PHP_CodeSniffer/src/Runner.php(400): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))
squizlabs#4 path/to/PHP_CodeSniffer/src/Runner.php(119): PHP_CodeSniffer\Runner->run()
squizlabs#5 path/to/PHP_CodeSniffer/bin/phpcs(30): PHP_CodeSniffer\Runner->runPHPCS()
squizlabs#6 {main}
  thrown in path/to/PHP_CodeSniffer/src/Fixer.php on line 144
```

To reproduce the issue (on `master`):
1. Create a small test file like:
    ```php
    <?php

    echo 'hello'.callMe( $p );
    ```
2. Run the following command to initialize the cache:
    ```bash
    phpcs -ps ./test.php --standard=PSR2 --report=Code,Diff --cache
    ```
    All should be fine.
3. Now run the same command again and see the fatal error.

Some investigating later, it turns out that both the `Code` report, as well as the `Diff` report, re-parse the current file, with the `Diff` report initializing the fixer once the file has reparsed, but the `Code` report not doing so (as it doesn't need the fixer).
The problem with that is that the `Code` report basically leaves the `Fixer` in an invalid state, leading to the above error.

While it is up for debate whether reports should ever (be allowed to) re-parse files, for now, let's fix the fatal.

Re-evaluating the report setup should definitely be done, but should probably wait until the Reports classes have test coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant