Completed
Push — dev ( 46c257...e6e7dc )
by Zach
03:51
created

HasLinks   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 66
loc 66
rs 10
wmc 7
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
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\Traits;
4
5 View Code Duplication
trait HasLinks
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
6
{
7
    /**
8
     * A project has many links.
9
     *
10
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
11
     */
12
    public function links()
13
    {
14
        return $this->hasMany(Link::class);
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
15
    }
16
    
17
    /**
18
     * Return true if project has links.
19
     *
20
     * @return bool
21
     */
22
    public function hasLinks()
23
    {
24
        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...
25
    }
26
27
    /**
28
     * Get link by name, if exists.
29
     *
30
     * @param string $name Name of link to get.
31
     *
32
     * @return Larafolio\Models\Link|null
33
     */
34
    public function link($name)
35
    {
36
        return $this->getFromRelationshipByName('links', $name);
0 ignored issues
show
Bug introduced by
It seems like getFromRelationshipByName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
    }
38
39
    /**
40
     * Get link url.
41
     *
42
     * @param string $name Name of link.
43
     *
44
     * @return string|null
45
     */
46
    public function linkUrl($name)
47
    {
48
        if (!$link = $this->link($name)) {
49
            return;
50
        }
51
52
        return $link->url();
53
    }
54
55
    /**
56
     * Get link text.
57
     *
58
     * @param string $name Name of link.
59
     *
60
     * @return string|null
61
     */
62
    public function linkText($name)
63
    {
64
        if (!$link = $this->link($name)) {
65
            return;
66
        }
67
68
        return $link->text();
69
    }
70
}
71