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 |
||
| 9 | class GeneratedValue implements Buildable |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var FieldBuilder |
||
| 13 | */ |
||
| 14 | protected $builder; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $strategy = 'AUTO'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $name; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $initial = 1; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $size = 10; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $generator; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ClassMetadataInfo |
||
| 43 | */ |
||
| 44 | protected $classMetadata; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param FieldBuilder $builder |
||
| 48 | * @param ClassMetadataInfo $classMetadata |
||
| 49 | */ |
||
| 50 | 26 | public function __construct(FieldBuilder $builder, ClassMetadataInfo $classMetadata) |
|
| 51 | { |
||
| 52 | 26 | $this->builder = $builder; |
|
| 53 | 26 | $this->classMetadata = $classMetadata; |
|
| 54 | 26 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Tells Doctrine to pick the strategy that is preferred by the used database platform. The preferred strategies |
||
| 58 | * are IDENTITY for MySQL, SQLite, MsSQL and SQL Anywhere and SEQUENCE for Oracle and PostgreSQL. This strategy |
||
| 59 | * provides full portability. |
||
| 60 | * |
||
| 61 | * @param string|null $name |
||
| 62 | * @param string|null $initial |
||
| 63 | * @param string|null $size |
||
| 64 | * |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | 6 | View Code Duplication | public function auto($name = null, $initial = null, $size = null) |
| 68 | { |
||
| 69 | 6 | $this->strategy = 'AUTO'; |
|
| 70 | 6 | $this->customize($name, $initial, $size); |
|
| 71 | |||
| 72 | 6 | return $this; |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Tells Doctrine to use a database sequence for ID generation. This strategy does currently not provide full |
||
| 77 | * portability. Sequences are supported by Oracle, PostgreSql and SQL Anywhere. |
||
| 78 | * |
||
| 79 | * @param string|null $name |
||
| 80 | * @param string|null $initial |
||
| 81 | * @param string|null $size |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | 6 | View Code Duplication | public function sequence($name = null, $initial = null, $size = null) |
| 86 | { |
||
| 87 | 6 | $this->strategy = 'SEQUENCE'; |
|
| 88 | 6 | $this->customize($name, $initial, $size); |
|
| 89 | |||
| 90 | 6 | return $this; |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Tells Doctrine to use special identity columns in the database that generate a value on insertion of a row. |
||
| 95 | * This strategy does currently not provide full portability and is supported by the following platforms: |
||
| 96 | * MySQL/SQLite/SQL Anywhere (AUTO_INCREMENT), MSSQL (IDENTITY) and PostgreSQL (SERIAL). |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | 2 | public function identity() |
|
| 101 | { |
||
| 102 | 2 | $this->strategy = 'IDENTITY'; |
|
| 103 | |||
| 104 | 2 | return $this; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Tells Doctrine to use the built-in Universally Unique Identifier generator. |
||
| 109 | * This strategy provides full portability. |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | 1 | public function uuid() |
|
| 114 | { |
||
| 115 | 1 | $this->strategy = 'UUID'; |
|
| 116 | |||
| 117 | 1 | return $this; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Tells Doctrine that the identifiers are assigned (and thus generated) by your code. The assignment must take |
||
| 122 | * place before a new entity is passed to EntityManager#persist. |
||
| 123 | * NONE is the same as leaving off the @GeneratedValue entirely. |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | 1 | public function none() |
|
| 128 | { |
||
| 129 | 1 | $this->strategy = 'NONE'; |
|
| 130 | |||
| 131 | 1 | return $this; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Tells Doctrine to use a custom Generator class to generate identifiers. |
||
| 136 | * The given class must extend \Doctrine\ORM\Id\AbstractIdGenerator |
||
| 137 | * |
||
| 138 | * @param string $generatorClass |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | 1 | public function custom($generatorClass) |
|
| 143 | { |
||
| 144 | 1 | $this->strategy = 'CUSTOM'; |
|
| 145 | 1 | $this->generator = $generatorClass; |
|
| 146 | |||
| 147 | 1 | return $this; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string|null $name |
||
| 152 | * @param string|null $initial |
||
| 153 | * @param string|null $size |
||
| 154 | */ |
||
| 155 | 12 | private function customize($name, $initial, $size) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Execute the build process |
||
| 164 | */ |
||
| 165 | 26 | public function build() |
|
| 166 | { |
||
| 167 | 26 | $this->builder->generatedValue($this->strategy); |
|
| 168 | |||
| 177 | } |
||
| 178 |