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

HasImages::hasImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Larafolio\Models\Traits;
4
5
trait HasImages
6
{
7
    /**
8
     * A project has many images.
9
     *
10
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
11
     */
12
    public function images()
13
    {
14
        return $this->hasMany(Image::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 images.
19
     *
20
     * @return bool
21
     */
22
    public function hasImages()
23
    {
24
        return !$this->images->isEmpty();
0 ignored issues
show
Bug introduced by
The property images 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 image by name, if exists.
29
     *
30
     * @param string $name Name of image to get.
31
     *
32
     * @return Larafolio\Models\Image|null
33
     */
34
    public function image($name)
35
    {
36
        return $this->getFromRelationshipByName('images', $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 image url for given size.
41
     *
42
     * @param string $name Name of image to get url for.
43
     * @param string $size Size of image.
44
     *
45
     * @return string|null
46
     */
47
    public function imageUrl($name, $size = 'medium')
48
    {
49
        if (!$image = $this->image($name)) {
50
            return;
51
        }
52
53
        return $image->{$size}();
54
    }
55
56
    /**
57
     * Get caption for image.
58
     *
59
     * @param string $name Name of image to get caption for.
60
     *
61
     * @return string|null
62
     */
63
    public function imageCaption($name)
64
    {
65
        if (!$image = $this->image($name)) {
66
            return;
67
        }
68
69
        return $image->caption();
70
    }
71
72
    /**
73
     * Get alt for image.
74
     *
75
     * @param string $name Name of image to get caption for.
76
     *
77
     * @return string|null
78
     */
79
    public function imageAlt($name)
80
    {
81
        if (!$image = $this->image($name)) {
82
            return;
83
        }
84
85
        return $image->alt();
86
    }
87
}
88