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() |
||
| 34 | { |
||
| 35 | return $this->getEnvLang(); |
||
| 36 | } |
||
| 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() |
||
| 60 | { |
||
| 61 | return $this->type; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getUri() |
||
| 65 | { |
||
| 66 | return $this->resource->getUri(); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function getVocab() |
||
| 70 | { |
||
| 71 | return $this->vocab; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getVocabName() |
||
| 75 | { |
||
| 76 | return $this->vocab->getTitle(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function addSubMember($member, $lang = '') |
||
| 80 | { |
||
| 81 | $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel(); |
||
| 82 | $this->submembers[$label->getValue()] = $member; |
||
| 83 | $this->sortSubMembers(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getSubMembers() |
||
| 87 | { |
||
| 88 | if (empty($this->submembers)) { |
||
| 89 | return null; |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->submembers; |
||
| 93 | } |
||
| 94 | |||
| 95 | private function sortSubMembers() |
||
| 96 | { |
||
| 97 | if (!empty($this->submembers)) { |
||
| 98 | ksort($this->submembers); |
||
| 99 | } |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | public function isExternal() { |
||
| 107 | |||
| 108 | public function getNotation() |
||
| 109 | { |
||
| 110 | if ($this->vocab->getConfig()->showNotation() && $this->resource->get('skos:notation')) { |
||
| 111 | return $this->resource->get('skos:notation')->getValue(); |
||
| 112 | } |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | public function isReified() { |
||
| 119 | |||
| 120 | public function getReifiedPropertyValues() { |
||
| 121 | $ret = array(); |
||
| 122 | $props = $this->resource->properties(); |
||
| 123 | foreach($props as $prop) { |
||
| 124 | if ($prop !== 'rdf:value') { // shown elsewhere |
||
| 125 | $ret[$prop] = $this->resource->get($prop); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | return $ret; |
||
| 129 | } |
||
| 130 | |||
| 131 | } |
||
| 132 |
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.