Issues (121)

src/Model/Carrier/DefaultCarrier.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Inspirum\Balikobot\Model\Carrier;
6
7
use Inspirum\Arrayable\BaseModel;
8
use Inspirum\Balikobot\Model\Method\MethodCollection;
9
use function array_map;
10
11
/**
12
 * @extends \Inspirum\Arrayable\BaseModel<string,mixed>
13
 */
14
final class DefaultCarrier extends BaseModel implements Carrier
15
{
16
    /**
17
     * @param array<string,\Inspirum\Balikobot\Model\Method\MethodCollection> $methods
18
     */
19 13
    public function __construct(
20
        private readonly string $code,
21
        private readonly string $name,
22
        private readonly array $methods = [],
23
    ) {
24 13
    }
25
26 7
    public function getCode(): string
27
    {
28 7
        return $this->code;
29
    }
30
31 1
    public function getName(): string
32
    {
33 1
        return $this->name;
34
    }
35
36
    /** @inheritDoc */
37 1
    public function getMethods(): array
38
    {
39 1
        return $this->methods;
40
    }
41
42 2
    public function getMethodsForVersion(string $version): MethodCollection
43
    {
44 2
        return $this->methods[$version];
45
    }
46
47
    /** @inheritDoc */
48 3
    public function __toArray(): array
49
    {
50 3
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('code' => $... */ }, $this->methods)) returns the type array<string,array|string> which is incompatible with the return type mandated by Inspirum\Arrayable\BaseModel::__toArray() of Inspirum\Arrayable\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...
51 3
            'code' => $this->code,
52 3
            'name' => $this->name,
53 3
            'methods' => array_map(static fn (MethodCollection $methods) => $methods->__toArray(), $this->methods),
54 3
        ];
55
    }
56
}
57