Conditions | 16 |
Paths | 36 |
Total Lines | 41 |
Lines | 14 |
Ratio | 34.15 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
38 | public function getLabel($lang = '', $fallback = 'uri') |
||
39 | { |
||
40 | if ($this->clang) { |
||
41 | $lang = $this->clang; |
||
42 | } |
||
43 | if ($this->vocab->getConfig()->getLanguageOrder($lang)) { |
||
44 | View Code Duplication | foreach ($this->vocab->getConfig()->getLanguageOrder($lang) as $fallback) { |
|
|
|||
45 | if ($this->resource->label($fallback) !== null) { |
||
46 | return $this->resource->label($fallback); |
||
47 | } |
||
48 | // We need to check all the labels in case one of them matches a subtag of the current language |
||
49 | if ($this->resource->allLiterals('skos:prefLabel')) { |
||
50 | foreach($this->resource->allLiterals('skos:prefLabel') as $label) { |
||
51 | // the label lang code is a subtag of the UI lang eg. en-GB - create a new literal with the main language |
||
52 | if ($label !== null && strpos($label->getLang(), $fallback . '-') === 0) { |
||
53 | return EasyRdf\Literal::create($label, $fallback); |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if ($this->resource->label($lang) !== null) { // current language |
||
61 | return $this->resource->label($lang); |
||
62 | } elseif ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { // vocab default language |
||
63 | return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage()); |
||
64 | } elseif ($this->resource->label() !== null) { // any language |
||
65 | return $this->resource->label(); |
||
66 | } elseif ($this->resource->getLiteral('rdf:value', $lang) !== null) { // current language |
||
67 | return $this->resource->getLiteral('rdf:value', $lang); |
||
68 | } elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language |
||
69 | return $this->resource->getLiteral('rdf:value'); |
||
70 | } |
||
71 | |||
72 | if ($fallback == 'uri') { |
||
73 | // return uri if no label is found |
||
74 | $label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri(); |
||
75 | return $label; |
||
76 | } |
||
77 | return null; |
||
78 | } |
||
79 | |||
159 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.