Completed
Branch master (9645b9)
by Neomerx
02:19
created

LinkWithAliases   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A canBeShownAsString() 0 4 2
A getArrayRepresentation() 0 12 3
A hasAliases() 0 4 1
A getAliases() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Schema;
4
5
/**
6
 * Copyright 2015-2019 [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->getAliases();
80
        }
81
82 5
        return $linkRepresentation;
83
    }
84
85
    /**
86
     * @return bool
87
     */
88 5
    private function hasAliases(): bool
89
    {
90 5
        return $this->hasAliases;
91
    }
92
93
    /**
94
     * Get aliases.
95
     *
96
     * @return array
97
     */
98 5
    private function getAliases(): array
99
    {
100 5
        return $this->aliases;
101
    }
102
}
103