HasAddress::getAddressAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yajra\Address;
4
5
use Yajra\Address\Entities\City;
6
use Yajra\Address\Entities\Region;
7
use Yajra\Address\Entities\Province;
8
use Yajra\Address\Entities\Barangay;
9
10
/**
11
 * @property string address
12
 * @property string street
13
 * @property string region_id
14
 * @property Region region
15
 * @property string province_id
16
 * @property Province province
17
 * @property string city_id
18
 * @property City city
19
 * @property string barangay_id
20
 * @property Barangay barangay
21
 */
22
trait HasAddress
23
{
24
    /**
25
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
26
     */
27
    public function region()
28
    {
29
        return $this->belongsTo(config('address.model.region', Region::class), 'region_id', 'region_id')->withDefault();
0 ignored issues
show
Bug introduced by
It seems like belongsTo() 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...
30
    }
31
32
    /**
33
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34
     */
35
    public function province()
36
    {
37
        return $this->belongsTo(config('address.model.province', Province::class), 'province_id', 'province_id')->withDefault();
0 ignored issues
show
Bug introduced by
It seems like belongsTo() 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...
38
    }
39
40
    /**
41
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
42
     */
43
    public function city()
44
    {
45
        return $this->belongsTo(config('address.model.city', City::class), 'city_id', 'city_id')->withDefault();
0 ignored issues
show
Bug introduced by
It seems like belongsTo() 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...
46
    }
47
48
    /**
49
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
50
     */
51
    public function barangay()
52
    {
53
        return $this->belongsTo(config('address.model.barangay', Barangay::class), 'barangay_id', 'code')->withDefault();
0 ignored issues
show
Bug introduced by
It seems like belongsTo() 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...
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getAddressAttribute()
60
    {
61
        return sprintf("%s %s, %s, %s",
62
            $this->street,
63
            $this->barangay->name,
64
            $this->city->name,
65
            $this->province->name
66
        );
67
    }
68
}
69