Completed
Pull Request — develop (#194)
by Wachter
14:07
created

ArticleJsConfig::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ArticleBundle\Admin;
13
14
use Sulu\Bundle\AdminBundle\Admin\JsConfigInterface;
15
use Sulu\Bundle\ArticleBundle\Metadata\StructureTagTrait;
16
use Sulu\Component\Content\Compat\StructureManagerInterface;
17
18
/**
19
 * Provides js-configuration.
20
 */
21
class ArticleJsConfig implements JsConfigInterface
22
{
23
    use StructureTagTrait;
24
25
    /**
26
     * @var StructureManagerInterface
27
     */
28
    private $structureManager;
29
30
    /**
31
     * @var array
32
     */
33
    private $typeConfiguration;
34
35
    /**
36
     * @var bool
37
     */
38
    private $displayTabAll;
39
40
    /**
41
     * @param StructureManagerInterface $structureManager
42
     * @param array $typeConfiguration
43
     * @param bool $displayTabAll
44
     */
45
    public function __construct(
46
        StructureManagerInterface $structureManager,
47
        array $typeConfiguration,
48
        $displayTabAll
49
    ) {
50
        $this->structureManager = $structureManager;
51
        $this->typeConfiguration = $typeConfiguration;
52
        $this->displayTabAll = $displayTabAll;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getParameters()
59
    {
60
        $config = [
61
            'types' => [],
62
            'templates' => [],
63
            'displayTabAll' => $this->displayTabAll,
64
        ];
65
66
        foreach ($this->structureManager->getStructures('article') as $structure) {
67
            $type = $this->getType($structure->getStructure());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sulu\Component\Content\Compat\StructureInterface as the method getStructure() does only exist in the following implementations of said interface: Sulu\Bundle\ArticleBundl...Structure\ArticleBridge, Sulu\Bundle\ArticleBundl...cture\ArticlePageBridge, Sulu\Component\Content\Compat\Structure\PageBridge, Sulu\Component\Content\C...Structure\SnippetBridge, Sulu\Component\Content\C...ructure\StructureBridge.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
68
            if (!array_key_exists($type, $config['types'])) {
69
                $config['types'][$type] = [
70
                    'default' => $structure->getKey(),
71
                    'title' => $this->getTitle($type),
72
                ];
73
            }
74
75
            $config['templates'][$structure->getKey()] = [
76
                'multipage' => ['enabled' => $this->getMultipage($structure->getStructure())],
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sulu\Component\Content\Compat\StructureInterface as the method getStructure() does only exist in the following implementations of said interface: Sulu\Bundle\ArticleBundl...Structure\ArticleBridge, Sulu\Bundle\ArticleBundl...cture\ArticlePageBridge, Sulu\Component\Content\Compat\Structure\PageBridge, Sulu\Component\Content\C...Structure\SnippetBridge, Sulu\Component\Content\C...ructure\StructureBridge.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
77
            ];
78
        }
79
80
        return $config;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getName()
87
    {
88
        return 'sulu_article';
89
    }
90
91
    /**
92
     * Returns title for given type.
93
     *
94
     * @param string $type
95
     *
96
     * @return string
97
     */
98
    private function getTitle($type)
99
    {
100
        if (!array_key_exists($type, $this->typeConfiguration)) {
101
            return ucfirst($type);
102
        }
103
104
        return $this->typeConfiguration[$type]['translation_key'];
105
    }
106
}
107