|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. |
| 4 | + * |
| 5 | + * @package PHPCSUtils |
| 6 | + * @copyright 2019-2024 PHPCSUtils Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCSStandards/PHPCSUtils |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHPCSUtils\Exceptions; |
| 12 | + |
| 13 | +use PHPCSUtils\Exceptions\RuntimeException; |
| 14 | + |
| 15 | +/** |
| 16 | + * Exception for an invalid argument value passed. |
| 17 | + * |
| 18 | + * This exception should be used when the argument uses the correct type, but doesn't comply with |
| 19 | + * predefined restrictions, like an empty string being passed, when only a non-empty string is accepted |
| 20 | + * or a negative integer being passed when a positive integer is expected. |
| 21 | + * |
| 22 | + * {@internal This exception should probably extend the PHP native `InvalidArgumentException`, or the |
| 23 | + * PHP 8.0+ `ValueError`, but that would inhibit the use of this exception, as replacing existing |
| 24 | + * exceptions with this (better) one would then be a breaking change.} |
| 25 | + * |
| 26 | + * @since 1.1.0 |
| 27 | + */ |
| 28 | +final class ValueError extends RuntimeException |
| 29 | +{ |
| 30 | + |
| 31 | + /** |
| 32 | + * Create a new ValueError exception with a standardized start of the text. |
| 33 | + * |
| 34 | + * @param int $position The argument position in the function signature. 1-based. |
| 35 | + * @param string $name The argument name in the function signature. |
| 36 | + * @param string $message Arbitrary message text. |
| 37 | + * |
| 38 | + * @return \PHPCSUtils\Exceptions\ValueError |
| 39 | + */ |
| 40 | + public static function create($position, $name, $message) |
| 41 | + { |
| 42 | + $stack = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
| 43 | + |
| 44 | + return new self( |
| 45 | + \sprintf( |
| 46 | + '%s::%s(): The value of argument #%d (%s) %s.', |
| 47 | + $stack[1]['class'], |
| 48 | + $stack[1]['function'], |
| 49 | + $position, |
| 50 | + $name, |
| 51 | + $message |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
0 commit comments