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 |
||
| 8 | class TDataServicesType extends IsOK |
||
| 9 | { |
||
| 10 | use IsOKToolboxTrait; |
||
| 11 | /** |
||
| 12 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[] $schema |
||
| 13 | */ |
||
| 14 | private $schema = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @property string $maxDataServiceVersion |
||
| 18 | */ |
||
| 19 | private $maxDataServiceVersion; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @property string $dataServiceVersion |
||
| 23 | */ |
||
| 24 | private $dataServiceVersion; |
||
| 25 | |||
| 26 | public function __construct($maxDataServiceVersion = '3.0', $dataServiceVersion = '3.0') |
||
| 27 | { |
||
| 28 | if (!is_numeric($maxDataServiceVersion)) { |
||
| 29 | $msg = "Maximum service version must be numeric"; |
||
| 30 | throw new \InvalidArgumentException($msg); |
||
| 31 | } |
||
| 32 | if (!is_numeric($dataServiceVersion)) { |
||
| 33 | $msg = "Data service version must be numeric"; |
||
| 34 | throw new \InvalidArgumentException($msg); |
||
| 35 | } |
||
| 36 | |||
| 37 | if (floatval($maxDataServiceVersion) < floatval($dataServiceVersion)) { |
||
| 38 | $msg = "Data service version cannot be greater than maximum service version"; |
||
| 39 | throw new \InvalidArgumentException($msg); |
||
| 40 | } |
||
| 41 | $this->setDataServiceVersion($dataServiceVersion); |
||
| 42 | $this->setMaxDataServiceVersion($maxDataServiceVersion); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Gets as MaxDataServiceVersion |
||
| 47 | * |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public function getMaxDataServiceVersion() |
||
| 51 | { |
||
| 52 | return $this->maxDataServiceVersion; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Sets a new DataServiceVersion |
||
| 57 | * |
||
| 58 | * @param string $maxDataServiceVersion |
||
| 59 | * @return self |
||
| 60 | */ |
||
| 61 | public function setMaxDataServiceVersion($maxDataServiceVersion) |
||
| 62 | { |
||
| 63 | $maxDataValid = ['3.0', '4.0']; |
||
| 64 | if (!in_array($maxDataServiceVersion, $maxDataValid)) { |
||
| 65 | $msg = "Maximum data service version must be 3.0 or 4.0"; |
||
| 66 | throw new \InvalidArgumentException($msg); |
||
| 67 | } |
||
| 68 | $this->maxDataServiceVersion = $maxDataServiceVersion; |
||
| 69 | return $this; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Gets as DataServiceVersion |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getDataServiceVersion() |
||
| 78 | { |
||
| 79 | return $this->dataServiceVersion; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Sets a new DataServiceVersion |
||
| 84 | * |
||
| 85 | * @param string $dataServiceVersion |
||
| 86 | * @return self |
||
| 87 | */ |
||
| 88 | public function setDataServiceVersion($dataServiceVersion) |
||
| 89 | { |
||
| 90 | $dataValid = ['1.0', '2.0', '3.0', '4.0']; |
||
| 91 | if (!in_array($dataServiceVersion, $dataValid)) { |
||
| 92 | $msg = "Data service version must be 1.0, 2.0, 3.0 or 4.0"; |
||
| 93 | throw new \InvalidArgumentException($msg); |
||
| 94 | } |
||
| 95 | $this->dataServiceVersion = $dataServiceVersion; |
||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | /** |
||
| 99 | * Adds as schema |
||
| 100 | * |
||
| 101 | * @return self |
||
| 102 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema $schema |
||
| 103 | */ |
||
| 104 | public function addToSchema(Schema $schema) |
||
| 105 | { |
||
| 106 | $msg = null; |
||
| 107 | if (!$schema->isOK($msg)) { |
||
| 108 | throw new \InvalidArgumentException($msg); |
||
| 109 | } |
||
| 110 | $this->schema[] = $schema; |
||
| 111 | return $this; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * isset schema |
||
| 116 | * |
||
| 117 | * @param scalar $index |
||
| 118 | * @return boolean |
||
| 119 | */ |
||
| 120 | public function issetSchema($index) |
||
| 121 | { |
||
| 122 | return isset($this->schema[$index]); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * unset schema |
||
| 127 | * |
||
| 128 | * @param scalar $index |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | public function unsetSchema($index) |
||
| 132 | { |
||
| 133 | unset($this->schema[$index]); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Gets as schema |
||
| 138 | * |
||
| 139 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[] |
||
| 140 | */ |
||
| 141 | public function getSchema() |
||
| 142 | { |
||
| 143 | return $this->schema; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Sets a new schema |
||
| 148 | * |
||
| 149 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\Schema[] $dataServices |
||
| 150 | * @return self |
||
| 151 | */ |
||
| 152 | public function setSchema(array $dataServices) |
||
| 153 | { |
||
| 154 | if (!$this->isValidArrayOK( |
||
|
|
|||
| 155 | $dataServices, |
||
| 156 | <<<<<<< HEAD |
||
| 157 | '\AlgoWeb\ODataMetadata\MetadataV3\edm\Schema', |
||
| 158 | $msg, |
||
| 159 | 1 |
||
| 160 | ======= |
||
| 161 | '\AlgoWeb\ODataMetadata\MetadataV3\edm\Schema' |
||
| 162 | >>>>>>> Code Cleanup |
||
| 163 | ) |
||
| 164 | ) { |
||
| 165 | $msg = "Data services array not a valid array"; |
||
| 166 | throw new \InvalidArgumentException($msg); |
||
| 167 | } |
||
| 168 | $this->schema = $dataServices; |
||
| 169 | return $this; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function isOK(&$msg = null) |
||
| 173 | { |
||
| 174 | if ('3.0' == $this->maxDataServiceVersion && '4.0' == $this->dataServiceVersion) { |
||
| 175 | $msg = "Data service version cannot be greater than maximum service version"; |
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (!$this->isValidArrayOK($this->schema, '\AlgoWeb\ODataMetadata\MetadataV3\edm\Schema', $msg, 1)) { |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | |||
| 186 |