| Conditions | 14 |
| Paths | 64 |
| Total Lines | 73 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 46 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 47 | { |
||
| 48 | $tokens = $phpcsFile->getTokens(); |
||
| 49 | |||
| 50 | if (strtolower($tokens[$stackPtr]['content']) !== 'class') { |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
||
| 55 | if ($prevToken === false || $tokens[$prevToken]['code'] !== T_DOUBLE_COLON) { |
||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($this->supportsBelow('5.4')) { |
||
| 60 | $phpcsFile->addError( |
||
| 61 | 'The magic class constant ClassName::class was not available in PHP 5.4 or earlier', |
||
| 62 | $stackPtr, |
||
| 63 | 'Found' |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | /* |
||
| 68 | * Check against invalid use of the magic `::class` constant. |
||
| 69 | */ |
||
| 70 | if ($this->supportsAbove('5.5') === false) { |
||
| 71 | return; |
||
| 72 | } |
||
| 73 | |||
| 74 | $classNameToken = $phpcsFile->findPrevious(T_STRING, ($prevToken - 1), null, false, null, true); |
||
| 75 | |||
| 76 | // Useless if not in a namespace. |
||
| 77 | $hasNamespace = false; |
||
| 78 | if ($classNameToken !== false) { |
||
| 79 | $namespace = $this->determineNamespace($phpcsFile, $classNameToken); |
||
| 80 | if (empty($namespace) === false) { |
||
| 81 | $hasNamespace = true; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | if ($hasNamespace === false) { |
||
| 86 | $phpcsFile->addWarning( |
||
| 87 | 'Using the magic class constant ClassName::class is only useful in combination with a namespaced class', |
||
| 88 | $stackPtr, |
||
| 89 | 'NotInNamespace' |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | // Is the magic constant used in a file which actually contains the referenced class ? |
||
| 95 | if ($classNameToken === false) { |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | $targetClassName = $tokens[$classNameToken]['content']; |
||
| 100 | $classPtr = $stackPtr; |
||
| 101 | while ($classPtr > 0) { |
||
| 102 | $classPtr = $phpcsFile->findPrevious(T_CLASS, ($classPtr - 1)); |
||
| 103 | |||
| 104 | if ($classPtr !== false) { |
||
| 105 | $className = $phpcsFile->getDeclarationName($classPtr); |
||
| 106 | if (!empty($className) && $className === $targetClassName) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | // Still here? In that case, the magic constant is used in a file which doesn't contain the target class. |
||
| 113 | $phpcsFile->addWarning( |
||
| 114 | 'The magic class constant ClassName::class can only be used in the same file as where the class is defined', |
||
| 115 | $stackPtr, |
||
| 116 | 'FileDoesNotContainClass' |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | } |
||
| 120 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.