Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 14 | |||
| 15 | final class DoctrineOrmExtension extends AbstractExtension |
||
| 16 | { |
||
| 17 | private ManagerRegistry $doctrine; |
||
|
|
|||
| 18 | |||
| 19 | 4 | public function __construct(ManagerRegistry $doctrine) |
|
| 20 | { |
||
| 21 | 4 | $this->doctrine = $doctrine; |
|
| 22 | 4 | } |
|
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | */ |
||
| 27 | 3 | public function getFunctions(): array |
|
| 28 | { |
||
| 29 | return [ |
||
| 30 | new TwigFunction('table_name', \Closure::bind(function ($entity) { |
||
| 31 | 1 | return $this->getTableName($entity); |
|
| 32 | 3 | }, $this)), |
|
| 33 | new TwigFunction('join_table_name', \Closure::bind(function ($field, $entity) { |
||
| 34 | return $this->getJoinTableName($field, $entity); |
||
| 35 | 3 | }, $this)), |
|
| 36 | new TwigFunction('column_name', \Closure::bind(function ($field, $entity) { |
||
| 37 | 1 | return $this->getColumnName($field, $entity); |
|
| 38 | 3 | }, $this)), |
|
| 39 | ]; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * {@inheritdoc} |
||
| 44 | */ |
||
| 45 | 1 | public function getFilters(): array |
|
| 46 | { |
||
| 47 | return [ |
||
| 48 | new TwigFilter('table_name', \Closure::bind(function ($entity) { |
||
| 49 | return $this->getTableName($entity); |
||
| 50 | 1 | }, $this)), |
|
| 51 | new TwigFilter('join_table_name', \Closure::bind(function ($field, $entity) { |
||
| 52 | return $this->getJoinTableName($field, $entity); |
||
| 53 | 1 | }, $this)), |
|
| 54 | new TwigFilter('column_name', \Closure::bind(function ($field, $entity) { |
||
| 55 | return $this->getColumnName($field, $entity); |
||
| 56 | 1 | }, $this)), |
|
| 57 | ]; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get table name for entity |
||
| 62 | * |
||
| 63 | * @param string $entity Entity |
||
| 64 | * |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | 1 | private function getTableName(string $entity): string |
|
| 68 | { |
||
| 69 | /** @var EntityManagerInterface $entityManager */ |
||
| 70 | 1 | $entityManager = $this->doctrine->getManagerForClass($entity); |
|
| 71 | |||
| 72 | 1 | return $entityManager->getClassMetadata($entity)->getTableName(); |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get column name for property of the entity |
||
| 77 | * |
||
| 78 | * @param string $field Entity property |
||
| 79 | * @param string $entity Entity |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 1 | private function getColumnName(string $field, string $entity): string |
|
| 84 | { |
||
| 85 | /** @var EntityManagerInterface $entityManager */ |
||
| 86 | 1 | $entityManager = $this->doctrine->getManagerForClass($entity); |
|
| 87 | |||
| 88 | 1 | return $entityManager->getClassMetadata($entity)->getColumnName($field); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get join table name name for property of the entity |
||
| 93 | * |
||
| 94 | * @param string $field Entity property |
||
| 95 | * @param string $entity Entity |
||
| 112 |