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

✨ Introduce 7 new exceptions #598

Merged
merged 7 commits into from
May 20, 2024
Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented May 13, 2024

These exceptions are primarily for use by PHPCSUtils itself, though could be used for utilities in external standards as well.

All exceptions extend the PHPCS native RuntimeException, which means replacing existing exceptions being thrown with the new exceptions will not cause a breaking change as catch( PHP_CodeSniffer\Exceptions\RuntimeException $e ) will still catch the new exceptions.

Having said that, once the new exceptions are in use, it also allows for making the catch more specific, which should allow for surfacing errors in sniffs which were accidentally caught.

✨ New PHPCSUtils native RuntimeException class

This exception extends the PHPCS native RuntimeException to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

This exception is also not final to allow more specific exceptions to extend from it. This is, again, deliberate, to allow pre-existing PHPCSUtils code to switch to more specific child-exceptions without causing a breaking change.

Includes changing the InvalidTokenArray exception to extend this new exception.

✨ New PHPCSUtils\Exceptions\TypeError exception class

New exception to flag an invalid argument type passed to a method using a standardized message.

This exception extends the PHPCSUtils native RuntimeException to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

If, by the time a 2.0 release happens, usage of this exception has not been replaced with PHP native type declarations yet, it should be considered to switch the parent class for the exception to the PHP native TypeError class (PHP 7.0+), which this exception largely emulates.

Includes perfunctory test for the exception.

✨ New PHPCSUtils\Exceptions\ValueError exception class

New exception to flag arguments using the correct type, but where the value doesn't comply with predefined restrictions, like an empty string being passed, when only a non-empty string is accepted or a negative integer being passed when a positive integer is expected.

The exception ensures these issues are flagged with a more consistent message format using a standardized message prefix.

This exception extends the PHPCSUtils native RuntimeException to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

By rights, this exception should extend the PHP native ValueError class, but unfortunately, that class is not available until PHP 8.0 (and not extending the RuntimeException would be a breaking change).

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native ValueError (PHP 8.0+) if the minimum supported PHP version allows for it.

Includes perfunctory test for the exception.

✨ New PHPCSUtils\Exceptions\OutOfBoundsStackPtr exception class

New exception to flag a passed stack pointer which doesn't exist in the $phpcsFile.

This exception extends the PHPCSUtils native RuntimeException to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change, though extending the PHPCSUtils ValueError class would also have been an option.

By rights, this exception should probably extend the PHP native OutOfBoundsException class, but not extending the RuntimeException would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native OutOfBoundsException.

Includes perfunctory test for the exception.

✨ New PHPCSUtils\Exceptions\UnexpectedTokenType exception class

New exception to flag a passed stack pointer argument, which does not comply with the token type requirements of the receiving method.

This exception extends the PHPCSUtils native RuntimeException to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change, though extending the PHPCSUtils ValueError class would also have been an option.

By rights, this exception should probably extend the PHP native InvalidArgumentException class, but not extending the RuntimeException would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native InvalidArgumentException.

Includes perfunctory test for the exception.

✨ New PHPCSUtils\Exceptions\LogicException class

New exception to flag an error in the program logic.

The exception ensures these issues are flagged with a more consistent message format using a standardized message prefix.

This exception extends the PHPCS native RuntimeException to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

By rights, this exception should extend the PHP native LogicException class, but not extending the RuntimeException would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native LogicException.

Includes perfunctory test for the exception.

✨ New PHPCSUtils\Exceptions\MissingArgumentError exception class

New exception to flag missing, conditionally required, parameters.

The exception ensures these issues are flagged with a more consistent message format using a standardized message prefix.

This exception extends the PHPCS native RuntimeException to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

By rights, this exception should extend the PHP native ArgumentCountError class, but not extending the RuntimeException would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native ArgumentCountError.

Includes perfunctory test for the exception.

jrfnl added 7 commits May 20, 2024 20:17
This exception extends the PHPCS native `RuntimeException` to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

This exception is also not `final` to allow more specific exceptions to extend from it. This is, again, deliberate, to allow pre-existing PHPCSUtils code to switch to more specific child-exceptions without causing a breaking change.

Includes changing the `InvalidTokenArray` exception to extend this new exception.
New exception to flag an invalid argument type passed to a method using a standardized message.

This exception extends the PHPCSUtils native `RuntimeException` to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

If, by the time a 2.0 release happens, usage of this exception has not been replaced with PHP native type declarations yet, it should be considered to switch the parent class for the exception to the PHP native `TypeError` class (PHP 7.0+), which this exception largely emulates.

Includes perfunctory test for the exception.
New exception to flag arguments using the correct type, but where the value doesn't comply with predefined restrictions, like an empty string being passed, when only a non-empty string is accepted or a negative integer being passed when a positive integer is expected.

The exception ensures these issues are flagged with a more consistent message format using a standardized message prefix.

This exception extends the PHPCSUtils native `RuntimeException` to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

By rights, this exception should extend the PHP native `ValueError` class, but unfortunately, that class is not available until PHP 8.0 (and not extending the `RuntimeException` would be a breaking change).

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native `ValueError` (PHP 8.0+) if the minimum supported PHP version allows for it.

Includes perfunctory test for the exception.
New exception to flag a passed stack pointer which doesn't exist in the $phpcsFile.

This exception extends the PHPCSUtils native `RuntimeException` to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change, though extending the PHPCSUtils `ValueError` class would also have been an option.

By rights, this exception should probably extend the PHP native `OutOfBoundsException` class, but not extending the `RuntimeException` would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native `OutOfBoundsException`.

Includes perfunctory test for the exception.
New exception to flag a passed stack pointer argument, which does not comply with the token type requirements of the receiving method.

This exception extends the PHPCSUtils native `RuntimeException` to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change, though extending the PHPCSUtils `ValueError` class would also have been an option.

By rights, this exception should probably extend the PHP native `InvalidArgumentException` class, but not extending the `RuntimeException` would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native `InvalidArgumentException`.

Includes perfunctory test for the exception.
New exception to flag an error in the program logic.

The exception ensures these issues are flagged with a more consistent message format using a standardized message prefix.

This exception extends the PHPCS native `RuntimeException` to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

By rights, this exception should extend the PHP native `LogicException` class, but not extending the `RuntimeException` would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native `LogicException`.

Includes perfunctory test for the exception.
New exception to flag missing, conditionally required, parameters.

The exception ensures these issues are flagged with a more consistent message format using a standardized message prefix.

This exception extends the PHPCS native `RuntimeException` to allow pre-existing PHPCSUtils code to switch to this exception without causing a breaking change.

By rights, this exception should extend the PHP native `ArgumentCountError` class, but not extending the `RuntimeException` would be a breaking change.

By the time a 2.0 release happens, it should be considered to switch the parent class for the exception to the PHP native `ArgumentCountError`.

Includes perfunctory test for the exception.
@jrfnl
Copy link
Member Author

jrfnl commented May 20, 2024

Rebased without changes. Merging once the build passes.

@jrfnl jrfnl force-pushed the feature/introduce-better-exceptions branch from d06975f to 8b08c1e Compare May 20, 2024 18:18
@jrfnl jrfnl merged commit f2d8282 into develop May 20, 2024
54 checks passed
@jrfnl jrfnl deleted the feature/introduce-better-exceptions branch May 20, 2024 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant