From 5132789a62f3b6434619f295cc0b99fbcc9c4b1b Mon Sep 17 00:00:00 2001 From: Vasek Purchart Date: Fri, 22 Sep 2017 09:56:12 +0200 Subject: [PATCH] Error last argument is considered chainable --- .../Exceptions/ExceptionDeclarationSniff.php | 1 + .../Exceptions/ExceptionDeclarationSniffTest.php | 9 +++++++++ ...ErrorArgumentChainableConstructorException.php | 15 +++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/Sniffs/Exceptions/data/ErrorArgumentChainableConstructorException.php diff --git a/Consistence/Sniffs/Exceptions/ExceptionDeclarationSniff.php b/Consistence/Sniffs/Exceptions/ExceptionDeclarationSniff.php index cf4cb86..5fc0645 100644 --- a/Consistence/Sniffs/Exceptions/ExceptionDeclarationSniff.php +++ b/Consistence/Sniffs/Exceptions/ExceptionDeclarationSniff.php @@ -116,6 +116,7 @@ private function checkThatExceptionIsChainable(PhpCsFile $phpcsFile, int $classP if ( $lastArgument->getTypeHint() !== '\Throwable' && !StringHelper::endsWith($lastArgument->getTypeHint(), 'Exception') + && !StringHelper::endsWith($lastArgument->getTypeHint(), 'Error') ) { $phpcsFile->addError(sprintf( 'Exception is not chainable. It must have optional \Throwable as last constructor argument and has "%s".', diff --git a/tests/Sniffs/Exceptions/ExceptionDeclarationSniffTest.php b/tests/Sniffs/Exceptions/ExceptionDeclarationSniffTest.php index 880ae16..07e892c 100644 --- a/tests/Sniffs/Exceptions/ExceptionDeclarationSniffTest.php +++ b/tests/Sniffs/Exceptions/ExceptionDeclarationSniffTest.php @@ -153,6 +153,15 @@ public function testExceptionWithCustomExceptionArgumentIsChainable() $this->assertNoSniffError($resultFile, 10); } + public function testExceptionWithErrorArgumentIsChainable() + { + $resultFile = $this->checkFile(__DIR__ . '/data/ErrorArgumentChainableConstructorException.php', [ + 'exceptionsDirectoryName' => 'data', + ]); + + $this->assertNoSniffError($resultFile, 10); + } + public function testExceptionWithNonchainableConstructorIsNotChainable() { $resultFile = $this->checkFile(__DIR__ . '/data/NonChainableConstructorException.php', [ diff --git a/tests/Sniffs/Exceptions/data/ErrorArgumentChainableConstructorException.php b/tests/Sniffs/Exceptions/data/ErrorArgumentChainableConstructorException.php new file mode 100644 index 0000000..55a14ea --- /dev/null +++ b/tests/Sniffs/Exceptions/data/ErrorArgumentChainableConstructorException.php @@ -0,0 +1,15 @@ +