|
| 1 | +<?php declare(strict_types=1); # -*- coding: utf-8 -*- |
| 2 | +/* |
| 3 | + * This file is part of the php-coding-standards package. |
| 4 | + * |
| 5 | + * (c) Inpsyde GmbH |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Inpsyde\InpsydeCodingStandard\Sniffs\CodeQuality; |
| 12 | + |
| 13 | +use Inpsyde\InpsydeCodingStandard\Helpers; |
| 14 | +use PHP_CodeSniffer\Files\File; |
| 15 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 16 | + |
| 17 | +/** |
| 18 | + * @package php-coding-standards |
| 19 | + * @license http://opensource.org/licenses/MIT MIT |
| 20 | + */ |
| 21 | +final class Psr4Sniff implements Sniff |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var string|null |
| 25 | + */ |
| 26 | + public $psr4 = null; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var array |
| 30 | + */ |
| 31 | + public $exclude = []; |
| 32 | + |
| 33 | + /** |
| 34 | + * @return int[] |
| 35 | + */ |
| 36 | + public function register() |
| 37 | + { |
| 38 | + return [T_CLASS, T_INTERFACE, T_TRAIT]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param File $file |
| 43 | + * @param int $position |
| 44 | + * @throws \PHP_CodeSniffer\Exceptions\RuntimeException |
| 45 | + */ |
| 46 | + public function process(File $file, $position) |
| 47 | + { |
| 48 | + $className = $file->getDeclarationName($position); |
| 49 | + $code = $file->getTokens()[$position]['code']; |
| 50 | + $entityType = 'class'; |
| 51 | + if ($code !== T_CLASS) { |
| 52 | + $entityType = $code === T_TRAIT ? 'trait' : 'interface'; |
| 53 | + } |
| 54 | + |
| 55 | + $this->exclude = $this->normalizeExcluded($this->exclude); |
| 56 | + |
| 57 | + $validNamespace = is_array($this->psr4) && $this->psr4 |
| 58 | + ? $this->checkNamespace($file, $position, $entityType, $className) |
| 59 | + : true; |
| 60 | + |
| 61 | + if (!$validNamespace) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $fileName = basename($file->getFilename()); |
| 66 | + if ($fileName === "{$className}.php") { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $file->addError( |
| 71 | + sprintf( |
| 72 | + "File containing %s '%s' is named '%s' instead of '%s'.", |
| 73 | + $entityType, |
| 74 | + $className, |
| 75 | + $fileName, |
| 76 | + "{$className}.php" |
| 77 | + ), |
| 78 | + $position, |
| 79 | + 'WrongFilename' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param File $file |
| 85 | + * @param int $position |
| 86 | + * @param string $entityType |
| 87 | + * @param string $className |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + private function checkNamespace( |
| 91 | + File $file, |
| 92 | + int $position, |
| 93 | + string $entityType, |
| 94 | + string $className |
| 95 | + ) { |
| 96 | + |
| 97 | + list($namespacePos, $namespace) = Helpers::findNamespace($file, $position); |
| 98 | + |
| 99 | + $fullyQualifiedName = "{$namespace}\\{$className}"; |
| 100 | + if (in_array($fullyQualifiedName, $this->exclude, true)) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + list($baseNamespace, $baseFolder) = $this->classPsr4Info($namespace); |
| 105 | + |
| 106 | + if (!$baseNamespace || !$namespacePos) { |
| 107 | + $file->addError( |
| 108 | + sprintf( |
| 109 | + "Namespace '%s' is not compliant with given PSR-4 configuration.", |
| 110 | + $namespace |
| 111 | + ), |
| 112 | + $namespacePos, |
| 113 | + 'NotInPSR4' |
| 114 | + ); |
| 115 | + |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + $namespaceRemain = trim(substr($namespace, strlen($baseNamespace)), '\\'); |
| 120 | + $expectedDirChunks = explode('\\', $namespaceRemain); |
| 121 | + array_unshift($expectedDirChunks, $baseFolder); |
| 122 | + |
| 123 | + $classPath = dirname($file->getFilename()); |
| 124 | + $classDirChunks = explode('/', str_replace('\\', '/', $classPath)); |
| 125 | + $actualDirChunks = array_slice($classDirChunks, -1 * count($expectedDirChunks)); |
| 126 | + |
| 127 | + if ($expectedDirChunks === $actualDirChunks) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + $file->addError( |
| 132 | + sprintf( |
| 133 | + "%s '%s', located in folder '%s', is not compliant with PSR-4 configuration.", |
| 134 | + ucfirst($entityType), |
| 135 | + $fullyQualifiedName, |
| 136 | + $classPath |
| 137 | + ), |
| 138 | + $namespacePos, |
| 139 | + 'InvalidPSR4' |
| 140 | + ); |
| 141 | + |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param array $excluded |
| 147 | + * @return array |
| 148 | + */ |
| 149 | + private function normalizeExcluded(array $excluded): array |
| 150 | + { |
| 151 | + return array_map( |
| 152 | + function (string $className): string { |
| 153 | + return ltrim($className, '\\'); |
| 154 | + }, |
| 155 | + $excluded |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param string $namespace |
| 161 | + * @return array |
| 162 | + */ |
| 163 | + private function classPsr4Info(string $namespace): array |
| 164 | + { |
| 165 | + $classBaseNamespace = null; |
| 166 | + $classBaseFolder = null; |
| 167 | + foreach ($this->psr4 as $baseNamespace => $folder) { |
| 168 | + $baseNamespace = trim($baseNamespace, '\\'); |
| 169 | + if (strpos($namespace, $baseNamespace) === 0) { |
| 170 | + $classBaseNamespace = $baseNamespace; |
| 171 | + $classBaseFolder = trim(str_replace('\\', '/', $folder), './'); |
| 172 | + break; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return [$classBaseNamespace, $classBaseFolder]; |
| 177 | + } |
| 178 | +} |
0 commit comments