RunOpenCode /
doctrine-naming-strategy-bundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy; |
||
| 6 | |||
| 7 | use Doctrine\ORM\Mapping\NamingStrategy; |
||
| 8 | use RunOpenCode\Bundle\DoctrineNamingStrategy\Exception\RuntimeException; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @psalm-suppress UnusedClass |
||
| 12 | */ |
||
| 13 | final class UnderscoredClassNamespacePrefix implements NamingStrategy |
||
| 14 | { |
||
| 15 | private int $case; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 16 | |||
| 17 | protected bool $joinTableFieldSuffix; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string[] |
||
| 21 | */ |
||
| 22 | protected array $whitelist; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string[] |
||
| 26 | */ |
||
| 27 | protected array $blacklist; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array<string, string> |
||
| 31 | */ |
||
| 32 | protected array $map; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @psalm-param array{case?: int, map?: array<string, string>, blacklist?: string[], whitelist?: string[], join_table_field_suffix?: bool } $configuration |
||
| 36 | * |
||
| 37 | * @throws RuntimeException |
||
| 38 | */ |
||
| 39 | 25 | public function __construct(array $configuration = []) |
|
| 40 | { |
||
| 41 | /** |
||
| 42 | * @psalm-var array{case: int, map: array<string, string>, blacklist: string[], whitelist: string[], join_table_field_suffix: bool } $configuration |
||
| 43 | */ |
||
| 44 | 25 | $configuration = array_merge([ |
|
| 45 | 25 | 'case' => CASE_LOWER, |
|
| 46 | 'map' => [], |
||
| 47 | 'whitelist' => [], |
||
| 48 | 'blacklist' => [], |
||
| 49 | 'join_table_field_suffix' => true, |
||
| 50 | ], $configuration); |
||
| 51 | |||
| 52 | 25 | if (\count($configuration['whitelist']) > 0 && \count($configuration['blacklist']) > 0) { |
|
| 53 | 1 | throw new RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.'); |
|
| 54 | } |
||
| 55 | |||
| 56 | 24 | $this->case = $configuration['case']; |
|
| 57 | /** |
||
| 58 | * @psalm-suppress MixedPropertyTypeCoercion |
||
| 59 | */ |
||
| 60 | $this->map = \array_map(\Closure::bind(function (string $prefix) { |
||
| 61 | 17 | return $this->underscore($prefix); |
|
| 62 | 24 | }, $this), $configuration['map']); |
|
| 63 | $this->blacklist = \array_map(static function (string $class) { |
||
| 64 | 1 | return \ltrim($class, '\\'); |
|
| 65 | 24 | }, $configuration['blacklist']); |
|
| 66 | $this->whitelist = \array_map(static function (string $class) { |
||
| 67 | 1 | return \ltrim($class, '\\'); |
|
| 68 | 24 | }, $configuration['whitelist']); |
|
| 69 | |||
| 70 | 24 | $this->joinTableFieldSuffix = $configuration['join_table_field_suffix']; |
|
| 71 | 24 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | 14 | public function classToTableName($className): string |
|
| 77 | { |
||
| 78 | 14 | $prefix = $this->getTableNamePrefix($className); |
|
| 79 | 14 | $position = \strrpos($className, '\\'); |
|
| 80 | |||
| 81 | 14 | if (false !== $position) { |
|
| 82 | 14 | $className = \substr($className, ($position + 1)); |
|
| 83 | } |
||
| 84 | |||
| 85 | 14 | return $prefix . $this->underscore($className); |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | 7 | public function propertyToColumnName($propertyName, $className = null): string |
|
| 92 | { |
||
| 93 | 7 | return $this->underscore($propertyName); |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | 3 | public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null): string |
|
| 100 | { |
||
| 101 | 3 | return $this->underscore($propertyName) . '_' . $this->underscore($embeddedColumnName); |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | 6 | public function referenceColumnName(): string |
|
| 108 | { |
||
| 109 | 6 | return $this->case === CASE_UPPER ? 'ID' : 'id'; |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 2 | public function joinColumnName($propertyName): string |
|
| 116 | { |
||
| 117 | 2 | return $this->underscore($propertyName) . '_' . $this->referenceColumnName(); |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | 3 | public function joinTableName($sourceEntity, $targetEntity, $propertyName = null): string |
|
| 124 | { |
||
| 125 | 3 | $tableName = $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity); |
|
| 126 | 3 | $suffix = $this->joinTableFieldSuffix && null !== $propertyName ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : ''; |
|
| 127 | |||
| 128 | 3 | return $tableName . $suffix; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 2 | public function joinKeyColumnName($entityName, $referencedColumnName = null): string |
|
| 135 | { |
||
| 136 | 2 | return $this->classToTableName($entityName) . '_' . |
|
| 137 | 2 | ($referencedColumnName ? $this->underscore($referencedColumnName) : $this->referenceColumnName()); |
|
| 138 | } |
||
| 139 | |||
| 140 | 14 | private function getTableNamePrefix(string $className): string |
|
| 141 | { |
||
| 142 | 14 | $className = \ltrim($className, '\\'); |
|
| 143 | |||
| 144 | 14 | foreach ($this->blacklist as $blacklist) { |
|
| 145 | 1 | if (0 === \strpos($className, $blacklist)) { |
|
| 146 | 1 | return ''; |
|
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | 13 | foreach ($this->map as $namespace => $prefix) { |
|
| 151 | 13 | if (0 === \strpos($className, $namespace)) { |
|
| 152 | 13 | foreach ($this->whitelist as $whitelistedNamespace) { |
|
| 153 | 1 | if (0 === \strpos($className, $whitelistedNamespace)) { |
|
| 154 | 1 | return $prefix . '_'; |
|
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | 12 | return 0 === \count($this->whitelist) ? ($prefix . '_') : ''; |
|
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | 6 | return ''; |
|
| 163 | } |
||
| 164 | |||
| 165 | 23 | private function underscore(string $literal): string |
|
| 166 | { |
||
| 167 | /** @var string $literal */ |
||
| 168 | 23 | $literal = \preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $literal); |
|
| 169 | |||
| 170 | 23 | if (CASE_UPPER === $this->case) { |
|
| 171 | 14 | return \strtoupper($literal); |
|
| 172 | } |
||
| 173 | |||
| 174 | 17 | return \strtolower($literal); |
|
| 175 | } |
||
| 176 | } |
||
| 177 |