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 | use Symfony\Component\HttpKernel\KernelInterface; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @psalm-suppress UnusedClass |
||
| 13 | */ |
||
| 14 | final class UnderscoredBundleNamePrefix implements NamingStrategy |
||
| 15 | { |
||
| 16 | private int $case; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 17 | |||
| 18 | private bool $joinTableFieldSuffix; |
||
| 19 | |||
| 20 | /** @var array<string, string> */ |
||
| 21 | private array $map; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @psalm-param array{case?: int, map?: array<string, string>, blacklist?: string[], whitelist?: string[], join_table_field_suffix?: bool } $options |
||
| 25 | * |
||
| 26 | * @throws RuntimeException |
||
| 27 | * @throws \ReflectionException |
||
| 28 | */ |
||
| 29 | 25 | public function __construct(KernelInterface $kernel, array $options = []) |
|
| 30 | { |
||
| 31 | /** |
||
| 32 | * @psalm-var array{case: int, map: array<string, string>, blacklist: string[], whitelist: string[], join_table_field_suffix: bool } $options |
||
| 33 | */ |
||
| 34 | 25 | $options = \array_merge([ |
|
| 35 | 25 | 'case' => CASE_LOWER, |
|
| 36 | 'map' => [], |
||
| 37 | 'whitelist' => [], |
||
| 38 | 'blacklist' => [], |
||
| 39 | 'join_table_field_suffix' => true, |
||
| 40 | 25 | ], $options); |
|
| 41 | |||
| 42 | 25 | if (\count($options['whitelist']) > 0 && \count($options['blacklist']) > 0) { |
|
| 43 | 1 | throw new RuntimeException('You can use whitelist or blacklist or none of mentioned lists, but not booth.'); |
|
| 44 | } |
||
| 45 | |||
| 46 | 24 | $this->case = $options['case']; |
|
| 47 | 24 | $this->joinTableFieldSuffix = $options['join_table_field_suffix']; |
|
| 48 | 24 | $this->map = $this->getNamingMap($kernel, $options); |
|
| 49 | 24 | } |
|
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | 14 | public function classToTableName($className): string |
|
| 55 | { |
||
| 56 | 14 | $prefix = $this->getTableNamePrefix($className); |
|
| 57 | 14 | $position = \strpos($className, '\\'); |
|
| 58 | |||
| 59 | 14 | if (false !== $position) { |
|
| 60 | /** @psalm-suppress PossiblyFalseOperand */ |
||
| 61 | 14 | $className = \substr($className, \strrpos($className, '\\') + 1); |
|
| 62 | } |
||
| 63 | |||
| 64 | 14 | return $prefix . $this->underscore($className); |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | 5 | public function propertyToColumnName($propertyName, $className = null): string |
|
| 71 | { |
||
| 72 | 5 | return $this->underscore($propertyName); |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 3 | public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null): string |
|
| 79 | { |
||
| 80 | 3 | return $this->underscore($propertyName) . '_' . $this->underscore($embeddedColumnName); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 6 | public function referenceColumnName(): string |
|
| 87 | { |
||
| 88 | 6 | return $this->case === CASE_UPPER ? 'ID' : 'id'; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | 2 | public function joinColumnName($propertyName): string |
|
| 95 | { |
||
| 96 | 2 | return $this->underscore($propertyName) . '_' . $this->referenceColumnName(); |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | 3 | public function joinTableName($sourceEntity, $targetEntity, $propertyName = null): string |
|
| 103 | { |
||
| 104 | 3 | $tableName = $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity); |
|
| 105 | |||
| 106 | return |
||
| 107 | $tableName |
||
| 108 | . |
||
| 109 | 3 | (($this->joinTableFieldSuffix && null !== $propertyName) ? '_' . $this->propertyToColumnName($propertyName, $sourceEntity) : ''); |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 2 | public function joinKeyColumnName($entityName, $referencedColumnName = null): string |
|
| 116 | { |
||
| 117 | 2 | return $this->classToTableName($entityName) . '_' . |
|
| 118 | 2 | ($referencedColumnName ? $this->underscore($referencedColumnName) : $this->referenceColumnName()); |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @psalm-param array{map: array<string, string>, blacklist: string[], whitelist: string[] } $configuration |
||
| 123 | * |
||
| 124 | * @return array<string, string> |
||
| 125 | * |
||
| 126 | * @throws \ReflectionException |
||
| 127 | */ |
||
| 128 | 24 | private function getNamingMap(KernelInterface $kernel, array $configuration): array |
|
| 129 | { |
||
| 130 | 24 | $map = []; |
|
| 131 | |||
| 132 | 24 | foreach ($kernel->getBundles() as $bundle) { |
|
| 133 | 24 | $bundleName = $bundle->getName(); |
|
| 134 | |||
| 135 | 24 | if (\count($configuration['blacklist']) > 0 && \in_array($bundleName, $configuration['blacklist'], true)) { |
|
| 136 | 1 | continue; |
|
| 137 | } |
||
| 138 | |||
| 139 | 24 | if (\count($configuration['whitelist']) > 0 && !\in_array($bundleName, $configuration['whitelist'], true)) { |
|
| 140 | 1 | continue; |
|
| 141 | } |
||
| 142 | |||
| 143 | 24 | $bundleNamespace = (new \ReflectionClass(\get_class($bundle)))->getNamespaceName(); |
|
| 144 | |||
| 145 | 24 | if (isset($configuration['map'][$bundleName])) { |
|
| 146 | 16 | $map[$this->underscore($configuration['map'][$bundleName])] = $bundleNamespace; |
|
| 147 | 16 | continue; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** @var string $bundleName */ |
||
| 151 | 23 | $bundleName = \preg_replace('/Bundle$/', '', $bundleName); |
|
| 152 | |||
| 153 | 23 | if (isset($configuration['map'][$bundleName])) { |
|
| 154 | 16 | $map[$this->underscore($configuration['map'][$bundleName])] = $bundleNamespace; |
|
| 155 | 16 | continue; |
|
| 156 | } |
||
| 157 | |||
| 158 | 23 | $map[$this->underscore($bundleName)] = $bundleNamespace; |
|
| 159 | } |
||
| 160 | |||
| 161 | 24 | return $map; |
|
| 162 | } |
||
| 163 | |||
| 164 | 14 | private function getTableNamePrefix(string $className): string |
|
| 165 | { |
||
| 166 | 14 | $className = \ltrim($className, '\\'); |
|
| 167 | |||
| 168 | 14 | foreach ($this->map as $prefix => $namespace) { |
|
| 169 | 14 | if (0 === \strpos($className, $namespace)) { |
|
| 170 | 13 | return $prefix . '_'; |
|
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | 7 | return ''; |
|
| 175 | } |
||
| 176 | |||
| 177 | 24 | private function underscore(string $literal): string |
|
| 178 | { |
||
| 179 | /** @var string $literal */ |
||
| 180 | 24 | $literal = \preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $literal); |
|
| 181 | |||
| 182 | 24 | if (CASE_UPPER === $this->case) { |
|
| 183 | 15 | return \strtoupper($literal); |
|
| 184 | } |
||
| 185 | |||
| 186 | 18 | return \strtolower($literal); |
|
| 187 | } |
||
| 188 | } |
||
| 189 |