Completed
Push — dev ( 2d24b0...5cd7c1 )
by Zach
06:05
created

HasLinks   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 76
loc 76
c 0
b 0
f 0
rs 10
wmc 7
lcom 2
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
getFromRelationshipByName() 1 1 ?
A links() 4 4 1
A hasLinks() 4 4 1
A link() 4 4 1
A linkUrl() 8 8 2
A linkText() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Larafolio\Models\ContentTraits;
4
5
use Larafolio\Models\Link;
6
7 View Code Duplication
trait HasLinks
8
{
9
    /**
10
     * Get a model from a relationship by model name.
11
     *
12
     * @param string $relationship Name of relationship.
13
     * @param string $name         Name of model to get.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Model|null
16
     */
17
    abstract protected function getFromRelationshipByName($relationship, $name);
18
19
    /**
20
     * A resource has many text blocks.
21
     *
22
     * @return MorphMany
23
     */
24
    public function links()
25
    {
26
        return $this->morphMany(Link::class, 'resource');
27
    }
28
29
    /**
30
     * Return true if project has links.
31
     *
32
     * @return bool
33
     */
34
    public function hasLinks()
35
    {
36
        return !$this->links->isEmpty();
1 ignored issue
show
Bug introduced by
The property links does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
39
    /**
40
     * Get link by name, if exists.
41
     *
42
     * @param string $name Name of link to get.
43
     *
44
     * @return Larafolio\Models\Link|null
45
     */
46
    public function link($name)
47
    {
48
        return $this->getFromRelationshipByName('links', $name);
49
    }
50
51
    /**
52
     * Get link url.
53
     *
54
     * @param string $name Name of link.
55
     *
56
     * @return string|null
57
     */
58
    public function linkUrl($name)
59
    {
60
        if (!$link = $this->link($name)) {
61
            return;
62
        }
63
64
        return $link->url();
65
    }
66
67
    /**
68
     * Get link text.
69
     *
70
     * @param string $name Name of link.
71
     *
72
     * @return string|null
73
     */
74
    public function linkText($name)
75
    {
76
        if (!$link = $this->link($name)) {
77
            return;
78
        }
79
80
        return $link->text();
81
    }
82
}
83