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 |
||
| 6 | class ConceptPropertyValue extends VocabularyDataObject |
||
|
|
|||
| 7 | { |
||
| 8 | /** submembers */ |
||
| 9 | private $submembers; |
||
| 10 | /** property type */ |
||
| 11 | private $type; |
||
| 12 | /** content language */ |
||
| 13 | private $clang; |
||
| 14 | |||
| 15 | View Code Duplication | public function __construct($model, $vocab, $resource, $prop, $clang = '') |
|
| 16 | { |
||
| 17 | parent::__construct($model, $vocab, $resource); |
||
| 18 | $this->submembers = array(); |
||
| 19 | $this->type = $prop; |
||
| 20 | $this->clang = $clang; |
||
| 21 | } |
||
| 22 | |||
| 23 | public function __toString() |
||
| 24 | { |
||
| 25 | $label = is_string($this->getLabel()) ? $this->getLabel() : $this->getLabel()->getValue(); |
||
| 26 | if ($this->vocab->getConfig()->sortByNotation()) { |
||
| 27 | $label = $this->getNotation() . $label; |
||
| 28 | } |
||
| 29 | |||
| 30 | return $label; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getLang() |
||
| 37 | |||
| 38 | public function getLabel($lang = '') |
||
| 39 | { |
||
| 40 | if ($this->clang) { |
||
| 41 | $lang = $this->clang; |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($this->resource->label($lang) !== null) { // current language |
||
| 45 | return $this->resource->label($lang); |
||
| 46 | View Code Duplication | } elseif ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { // vocab default language |
|
| 47 | return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage()); |
||
| 48 | } elseif ($this->resource->label() !== null) { // any language |
||
| 49 | return $this->resource->label(); |
||
| 50 | } elseif ($this->resource->getLiteral('rdf:value', $lang) !== null) { // current language |
||
| 51 | return $this->resource->getLiteral('rdf:value', $lang); |
||
| 52 | } elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language |
||
| 53 | return $this->resource->getLiteral('rdf:value'); |
||
| 54 | } |
||
| 55 | $label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri(); |
||
| 56 | return $label; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getType() |
||
| 63 | |||
| 64 | public function getUri() |
||
| 68 | |||
| 69 | public function getVocab() |
||
| 73 | |||
| 74 | public function getVocabName() |
||
| 78 | |||
| 79 | public function addSubMember($member, $lang = '') |
||
| 80 | { |
||
| 81 | $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel(); |
||
| 85 | |||
| 86 | public function getSubMembers() |
||
| 94 | |||
| 95 | private function sortSubMembers() |
||
| 102 | |||
| 103 | public function isExternal() { |
||
| 107 | |||
| 108 | public function getNotation() |
||
| 115 | |||
| 116 | public function isReified() { |
||
| 119 | |||
| 120 | } |
||
| 121 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.