LinkWithAliases::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 5
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 4
rs 9.7998
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Schema;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface;
22
use Neomerx\JsonApi\Contracts\Schema\LinkWithAliasesInterface;
23
24
/**
25
 * @package Neomerx\JsonApi
26
 */
27
class LinkWithAliases extends Link implements LinkWithAliasesInterface
28
{
29
    /**
30
     * @var array
31
     */
32
    private $aliases;
33
34
    /**
35
     * @var bool
36
     */
37
    private $hasAliases;
38
39
    /**
40
     * @param bool     $isSubUrl
41
     * @param string   $value
42
     * @param iterable $aliases
43
     * @param bool     $hasMeta
44
     * @param null     $meta
45
     */
46 5
    public function __construct(bool $isSubUrl, string $value, iterable $aliases, bool $hasMeta, $meta = null)
47
    {
48 5
        $aliasesArray = [];
49 5
        foreach ($aliases as $name => $alias) {
50 5
            \assert(\is_string($name) === true && empty($name) === false);
51 5
            \assert(\is_string($alias) === true && empty($alias) === false);
52 5
            $aliasesArray[$name] = $alias;
53
        }
54
55 5
        $this->aliases    = $aliasesArray;
56 5
        $this->hasAliases = !empty($aliasesArray);
57
58 5
        parent::__construct($isSubUrl, $value, $hasMeta, $meta);
59 5
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 5
    public function canBeShownAsString(): bool
65
    {
66 5
        return parent::canBeShownAsString() && $this->hasAliases === false;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 5
    public function getArrayRepresentation(string $prefix): array
73
    {
74 5
        $linkRepresentation = parent::canBeShownAsString() === true ? [
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (canBeShownAsString() instead of getArrayRepresentation()). Are you sure this is correct? If so, you might want to change this to $this->canBeShownAsString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
75 5
            DocumentInterface::KEYWORD_HREF => $this->buildUrl($prefix),
76 5
        ] : parent::getArrayRepresentation($prefix);
77
78 5
        if ($this->hasAliases === true) {
79 5
            $linkRepresentation[DocumentInterface::KEYWORD_ALIASES] = $this->aliases;
80
        }
81
82 5
        return $linkRepresentation;
83
    }
84
}
85