Completed
Push — dev ( 8b58af...5d63f9 )
by Zach
03:45
created

HasLines::hasLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 4
loc 4
rs 10
1
<?php
2
3
namespace Larafolio\Models\ContentTraits;
4
5
use Larafolio\Models\TextLine;
6
7 View Code Duplication
trait HasLines
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...
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
     * Define a polymorphic one-to-many relationship.
21
     *
22
     * @param string $related
23
     * @param string $name
24
     * @param string $type
25
     * @param string $id
26
     * @param string $localKey
27
     *
28
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
29
     */
30
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
31
32
    /**
33
     * A resource has many text lines.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
36
     */
37
    public function lines()
38
    {
39
        return $this->morphMany(TextLine::class, 'resource');
40
    }
41
42
    /**
43
     * Return true if project has lines.
44
     *
45
     * @return bool
46
     */
47
    public function hasLines()
48
    {
49
        return !$this->lines->isEmpty();
1 ignored issue
show
Bug introduced by
The property lines 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...
50
    }
51
52
    /**
53
     * Get a text line by name, if exists.
54
     *
55
     * @param string $name Name of text line to get.
56
     *
57
     * @return \Larafolio\Models\TextBlock|null
58
     */
59
    public function line($name)
60
    {
61
        return $this->getFromRelationshipByName('lines', $name);
62
    }
63
64
    /**
65
     * Get line text by line name, if line exists.
66
     *
67
     * @param string $name      Name of text line to get.
68
     * @param bool   $formatted If true, return formmated text.
69
     *
70
     * @return string|null
71
     */
72
    public function lineText($name, $formatted = true)
73
    {
74
        if (!$line = $this->line($name)) {
75
            return;
76
        }
77
78
        if ($formatted) {
79
            return $line->formattedText();
80
        }
81
82
        return $line->text();
83
    }
84
}
85