Skip to content

Commit 8b08c1e

Browse files
committed
✨ 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.
1 parent 4a5c3da commit 8b08c1e

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 when a (conditionally) required parameter is not passed.
17+
*
18+
* {@internal This exception should probably extend the PHP native `ArgumentCountError`, but
19+
* that would inhibit the use of this exception, as replacing existing exceptions with this
20+
* (better) one would then be a breaking change.}
21+
*
22+
* @since 1.1.0
23+
*/
24+
final class MissingArgumentError extends RuntimeException
25+
{
26+
27+
/**
28+
* Create a new MissingArgumentError exception with a standardized start of the text.
29+
*
30+
* @param int $position The argument position in the function signature. 1-based.
31+
* @param string $name The argument name in the function signature.
32+
* @param string $message Arbitrary message text, which should indicate under what
33+
* conditions the parameter is required.
34+
*
35+
* @return \PHPCSUtils\Exceptions\MissingArgumentError
36+
*/
37+
public static function create($position, $name, $message)
38+
{
39+
$stack = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 2);
40+
41+
return new self(
42+
\sprintf(
43+
'%s::%s(): Argument #%d (%s) is required %s.',
44+
$stack[1]['class'],
45+
$stack[1]['function'],
46+
$position,
47+
$name,
48+
$message
49+
)
50+
);
51+
}
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Tests\Exceptions\MissingArgumentError;
12+
13+
use PHPCSUtils\Exceptions\MissingArgumentError;
14+
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
15+
16+
/**
17+
* Test class.
18+
*
19+
* @covers \PHPCSUtils\Exceptions\MissingArgumentError
20+
*
21+
* @since 1.1.0
22+
*/
23+
final class MissingArgumentErrorTest extends TestCase
24+
{
25+
26+
/**
27+
* Test that the text of the exception is as expected.
28+
*
29+
* @return void
30+
*/
31+
public function testCreate()
32+
{
33+
$message = \sprintf(
34+
'%s(): Argument #2 ($config) is required for PHPCS 4.x',
35+
__METHOD__
36+
);
37+
38+
$this->expectException('PHPCSUtils\Exceptions\MissingArgumentError');
39+
$this->expectExceptionMessage($message);
40+
41+
throw MissingArgumentError::create(2, '$config', 'for PHPCS 4.x');
42+
}
43+
}

0 commit comments

Comments
 (0)