HasUrlRewrite   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 30
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrlAttribute() 0 8 2
A getUrlRewriteAttributesArray() 0 10 2
A getUrlRewrite() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RuthgerIdema\UrlRewrite\Traits;
6
7
use RuthgerIdema\UrlRewrite\Facades\UrlRewrite;
8
9
trait HasUrlRewrite
10
{
11
    public function getUrlAttribute(): string
12
    {
13
        if (! $urlRewrite = $this->getUrlRewrite()) {
14
            return '';
15
        }
16
17
        return route('url.rewrite', $urlRewrite->request_path, false);
18
    }
19
20
    public function getUrlRewriteAttributesArray(): ?array
21
    {
22
        $mapped = [];
23
24
        foreach (config("url-rewrite.types.$this->urlRewriteType.attributes") as $attribute) {
0 ignored issues
show
Bug introduced by
The property urlRewriteType 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
            $mapped[$attribute] = $this->getAttribute($attribute);
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
26
        }
27
28
        return $mapped;
29
    }
30
31
    public function getUrlRewrite(): ?object
32
    {
33
        return UrlRewrite::getByTypeAndAttributes(
34
            config("url-rewrite.types.$this->urlRewriteType.route"),
35
            $this->getUrlRewriteAttributesArray()
36
        );
37
    }
38
}
39