Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EditorConfig and PHPStan #61

Closed
wants to merge 5 commits into from

Conversation

mcaskill
Copy link
Contributor

@mcaskill mcaskill commented Mar 16, 2023

Checklist

  • I've read the Contributing page.
  • No issue.
  • My code is tested.
  • My code follows the WordPress code style.
  • My code has proper inline documentation.

Description

Follow-up to #59 and fork of #58.

Depends on #58 being merged first.

Added an EditorConfig ruleset to enforce tabs for PHP files and spaces for JSON, Markdown, NEON, YML, and XML files. This should help avoid accidental mixing of tabs if one's IDE supports this feature.

Added PHPStan dependency to help finds bugs and logic flaws. Given that tests are absent and not so simple to write given the proprietary and expensive nature of its target WP plugins.

composer lint:phpstan -- --xdebug

PHPStan's situation is a little complicated given this plugin supports multiple versions of different packages (notably Composer and vlucas/phpdotenv). PHPStan can't tell that and reports errors for whichever version is not installed. To resolve this, I've introduced a way to conditionally include different baselines based on what is installed in order to hide "errors" from versions not installed.

This conditional functionality can be greatly reduced if we drop support for older versions of PHP, Composer, and other dependencies.

How has this been tested?

  • PHP 8.0
  • PHP 7.4
  • Composer 1.10
  • Composer 2.5
  • vlucas/phpdotenv 5.5
  • vlucas/phpdotenv 4.3
  • vlucas/phpdotenv 3.6

Types of changes

Enforces minimal coding styles to match existing coding styles inspired by WordPress' own styles.

Changed:
- Replaced inconsistent indentation in `RemoteFilesystem`.
- Removed colon in pull request template to match other titles.
As well as Composer to analyze source and catch potential issues.

Required:
- composer/composer ^1.0 or ^2.0
- phpstan/phpstan ^1.10

Usage:

```shell
composer lint:phpstan -- --xdebug
```
Changed:
- Throw `InvalidArgumentException` instead of `UnexpectedValueException` when dealing with an unsupported package in Ninja Forms, PublishPress Pro, and WPML.
- Throw `UnexpectedValueException` if the decoded/unserialized API response is not an array.
- Added PHPDoc tags to document exceptions.
- Fixed issues reported by PHPStan.
Changed:
- Added protected method `request()` to aggregate the execution of the request, handling of the response, and closing of the cURL handler.
- Throw `UnexpectedValueException` if the request failed or if the response is invalid or empty.
- Fixed issues reported by PHPStan.
Based on the PHP version and installed package versions.

Added conditionally included baselines for PHP 5–7 and 8+, Composer 1 and 2, Dotenv 3–5.

This conditional functionality can be greatly reduced if we drop support for older versions of PHP, Composer, and other dependencies.

To resolve these conditional baselines, we have to replace the initial data of `Composer\InstalledVersions` which contains PHPStan's dependencies, since the context is its PHAR.

To accomplish this, we have to retrieve the contents of the project's installed dependencies from either:

1. `vendor/composer/installed.php` — Composer v2 PHP format.
2. `vendor/composer/installed.json` — Either Composer v1 or v2 JSON format.

The JSON format is tricky because it changes drastically between Composer v1 and v2 and the v2 PHP format. Both JSON formats must be remapped to the PHP format expected by `InstalledVersions`.

If the project's installed dependencies cannot be loaded, this file returns only the PHP baselines.

If the project's installed dependencies can be loaded, the extra baselines are resolved and the initial data of `InstalledVersions` is restored.
@mcaskill mcaskill marked this pull request as ready for review July 11, 2024 22:27
@mcaskill
Copy link
Contributor Author

Accidentally marked the wrong pull request on the upstream repository instead of the fork 🤦‍♂️

@mcaskill mcaskill closed this Jul 11, 2024
mcaskill added a commit to wp-jazz/composer-wp-pro-plugins that referenced this pull request Jul 13, 2024
From from 'mcaskill/feature/linting':

Add EditorConfig and PHPStan
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