Passed
Push — main ( 01d627...8c0949 )
by Michael
03:55
created

ClassString   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 7
c 1
b 0
f 1
dl 0
loc 52
ccs 11
cts 11
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A interfaceExists() 0 3 1
A classExists() 0 3 1
A value() 0 3 1
A toArray() 0 3 1
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ValueObjects\Complex;
6
7
use Illuminate\Support\Traits\Conditionable;
8
use Illuminate\Support\Traits\Macroable;
9
use MichaelRubel\ValueObjects\ValueObject;
10
11
/**
12
 * @method static static make(string $classString)
13
 */
14
class ClassString extends ValueObject
15
{
16
    use Macroable, Conditionable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by MichaelRubel\ValueObjects\Complex\ClassString.
Loading history...
17
18
    /**
19
     * @param  string|null  $classString
20
     */
21 12
    public function __construct(protected ?string $classString)
22
    {
23
        //
24
    }
25
26
    /**
27
     * @return bool
28
     */
29 5
    public function classExists(): bool
30
    {
31 5
        return class_exists($this->value());
32
    }
33
34
    /**
35
     * @return bool
36
     */
37 5
    public function interfaceExists(): bool
38
    {
39 5
        return interface_exists($this->value());
40
    }
41
42
    /**
43
     * @return string
44
     */
45 10
    public function value(): string
46
    {
47 10
        return (string) $this->classString;
48
    }
49
50
    /**
51
     * @return array
52
     */
53 1
    public function toArray(): array
54
    {
55 1
        return [$this->value()];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($this->value()) returns the type array<integer,string> which is incompatible with the return type mandated by Illuminate\Contracts\Support\Arrayable::toArray() of Illuminate\Contracts\Support\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
56
    }
57
58
    /**
59
     * Get string representation of the value object.
60
     *
61
     * @return string
62
     */
63 2
    public function __toString(): string
64
    {
65 2
        return $this->value();
66
    }
67
}
68