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

ClassString::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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