-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Until now the SuperfluousExceptionNaming sniff was enabled in Doctrine Coding Standard, but every of our projects disabled this sniff in their local phpcs.xml rule. Doctrine does not actually follow this rule, instead we provide the context of the exception in the name as a "prefix" similar to PHPs own RuntimeException and so on. This is done, because exceptions are used soley in the context of code that reads like: } catch (DBALException $e) { } If we allow classes named "Exception", then we introduce a developer experience problem, because it potentially requires the user to make an alias/renaming decision, increasing their mental load: use OtherLibrary\Exception as OtherException; use Doctrine\DBAL\Exception as DBALException; use Exception; } catch (OtherException $e) { } catch (DBALException $e) { } catch (Exception $e) { } Additionally it makes it hard for developers understanding catch clauses when they cannot rely on the fact that "Exception" is the global one. } catch (Exception $e) { // don't mess with user expectations
- Loading branch information
Showing
6 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
lib/Doctrine/Sniffs/NamingConventions/ExceptionNamingSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Sniffs\NamingConventions; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
use function trim; | ||
use function ucfirst; | ||
|
||
use const T_CLASS; | ||
use const T_STRING; | ||
|
||
class ExceptionNamingSniff implements Sniff | ||
{ | ||
/** | ||
* @return array<int, (int|string)> | ||
* | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint | ||
*/ | ||
public function register() | ||
{ | ||
return [T_CLASS]; | ||
} | ||
|
||
/** | ||
* @param int $stackPtr | ||
* | ||
* @return void | ||
* | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
|
||
$className = $phpcsFile->findNext(T_STRING, $stackPtr); | ||
$name = trim($tokens[$className]['content']); | ||
$errorData = [ucfirst($tokens[$stackPtr]['content'])]; | ||
|
||
switch ($name) { | ||
case 'Exception': | ||
$phpcsFile->addError( | ||
'Using Exception as a short class name is not allowed.', | ||
$stackPtr, | ||
'Invalid', | ||
$errorData | ||
); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test; | ||
|
||
class Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Test; | ||
|
||
class Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters