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

HasBlocks::blockText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 12
loc 12
rs 9.4285
1
<?php
2
3
namespace Larafolio\Models\Traits;
4
5
use Larafolio\Models\TextBlock;
6
7 View Code Duplication
trait HasBlocks
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
     * A project has many text blocks.
11
     *
12
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
13
     */
14
    public function blocks()
15
    {
16
        return $this->hasMany(TextBlock::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...
17
    }
18
19
    /**
20
     * Return true if project has blocks.
21
     *
22
     * @return bool
23
     */
24
    public function hasBlocks()
25
    {
26
        return !$this->blocks->isEmpty();
1 ignored issue
show
Bug introduced by
The property blocks 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...
27
    }
28
    
29
    /**
30
     * Get a text block by name, if exists.
31
     *
32
     * @param string $name Name of text block to get.
33
     *
34
     * @return Larafolio\Models\TextBlock
35
     */
36
    public function block($name)
37
    {
38
        return $this->getFromRelationshipByName('blocks', $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...
39
    }
40
41
    /**
42
     * Get block text by block name, if block exists.
43
     *
44
     * @param string $name      Name of text block to get.
45
     * @param bool   $formatted If true, return formmated text.
46
     *
47
     * @return string|null
48
     */
49
    public function blockText($name, $formatted = true)
50
    {
51
        if (!$block = $this->block($name)) {
52
            return;
53
        }
54
55
        if ($formatted) {
56
            return $block->formattedText();
57
        }
58
59
        return $block->text();
60
    }
61
}
62