Completed
Push — master ( b26542...bb7d10 )
by Nikola
07:16
created

Configuration::getFileRepositoryDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.4286
cc 1
eloc 13
nc 1
nop 0
crap 2
1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection;
11
12
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14 4
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16 4
/**
17
 * Class Configuration
18 4
 *
19
 * Configuration tree.
20
 *
21 4
 * @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection
22 4
 */
23 4
class Configuration implements ConfigurationInterface
24 4
{
25 4
    /**
26 4
     * {@inheritdoc}
27 4
     */
28 4
    public function getConfigTreeBuilder()
29 4
    {
30
        $treeBuilder = new TreeBuilder();
31 4
32
        $rootNode = $treeBuilder->root('run_open_code_exchange_rate');
33
34 4
        $rootNode
35
            ->children()
36 4
                ->scalarNode('base_currency')
37
                    ->isRequired()
38
                    ->info('Set base currency in which you are doing your business activities.')
39 4
                ->end()
40 4
                ->scalarNode('repository')
41 4
                    ->defaultValue('run_open_code.exchange_rate.repository.file_repository')
42 4
                    ->info('Service ID which is in charge for rates persistence.')
43 4
                ->end()
44 4
                ->append($this->getRatesDefinition())
45 4
                ->append($this->getProcessorsDefinition())
46 4
                ->append($this->getFileRepositoryDefinition())
47 4
                ->append($this->getViewDefinition())
48
            ->end()
49 4
        ->end();
50
51
        return $treeBuilder;
52 4
    }
53
54 4
    /**
55
     * Build configuration tree for rates.
56
     *
57 4
     * @return ArrayNodeDefinition
58 4
     */
59 4
    protected function getRatesDefinition()
60
    {
61 4
        $node = new ArrayNodeDefinition('rates');
62
63
        $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method requiresAtLeastOneElement() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
64 4
            ->info('Configuration of each individual rate with which you intend to work with.')
65
            ->requiresAtLeastOneElement()
66 4
                ->prototype('array')
67
                    ->children()
68
                        ->scalarNode('currency_code')->isRequired()->end()
69 4
                        ->scalarNode('rate_type')->isRequired()->end()
70 4
                        ->scalarNode('source')->isRequired()->end()
71 4
                        ->scalarNode('alias')->defaultValue(null)->end()
72 4
                        ->arrayNode('extra')->end()
73 4
                    ->end()
74
                ->end()
75 4
            ->end();
76
77
        return $node;
78 4
    }
79
80 4
    /**
81
     * Build configuration tree for processors.
82
     *
83 4
     * @return ArrayNodeDefinition
84 4
     */
85 4
    protected function getProcessorsDefinition()
86 4
    {
87 4
        $node = new ArrayNodeDefinition('processors');
88 4
89 4
        $node
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. Did you maybe mean attribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90 4
            ->info('List of processors which ought to be executed after fetch process.')
91 4
            ->useAttributeAsKey('name')
92 4
                ->prototype('scalar')->end()
93
            ->end();
94 4
95
        return $node;
96
    }
97
98
    /**
99
     * Build configuration tree for repository.
100
     *
101
     * @return ArrayNodeDefinition
102
     */
103
    protected function getFileRepositoryDefinition()
104
    {
105
        $node = new ArrayNodeDefinition('file_repository');
106
107
        $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method addDefaultsIfNotSet() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
108
            ->info('Configuration for file repository (if used).')
109
            ->addDefaultsIfNotSet()
110
            ->children()
111
                ->scalarNode('path')
112
                ->info('Absolute path to file where database file will be stored.')
113
                ->defaultValue('%kernel.root_dir%/db/exchange_rates.dat')
114
                ->end()
115
            ->end()
116
        ->end();
117
118
        return $node;
119
    }
120
121
    /**
122
     * Build configuration tree for view (controller).
123
     *
124
     * @return ArrayNodeDefinition
125
     */
126
    protected function getViewDefinition()
127
    {
128
        $node = new ArrayNodeDefinition('view');
129
130
        $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method addDefaultsIfNotSet() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
131
            ->info('Configuration of administration interface.')
132
            ->addDefaultsIfNotSet()
133
            ->children()
134
                ->scalarNode('base_template')
135
                    ->info('Base decorator template.')
136
                    ->defaultValue('@ExchangeRate/base.html.twig')
137
                ->end()
138
                ->scalarNode('list')
139
                    ->info('Template for list view.')
140
                    ->defaultValue('@ExchangeRate/list.html.twig')
141
                ->end()
142
                ->scalarNode('new')
143
                    ->info('Template for create new exchange rate view.')
144
                    ->defaultValue('@ExchangeRate/new.html.twig')
145
                ->end()
146
                ->scalarNode('edit')
147
                    ->info('Template for edit exchange rate view.')
148
                    ->defaultValue('@ExchangeRate/edit.html.twig')
149
                ->end()
150
                ->scalarNode('date_format')
151
                    ->info('Date format in list view.')
152
                    ->defaultValue('Y-m-d')
153
                ->end()
154
                ->scalarNode('time_format')
155
                    ->info('Date/time format in list view.')
156
                    ->defaultValue('H:i')
157
                ->end()
158
                ->booleanNode('secure')->defaultValue(true)->end()
159
            ->end()
160
        ->end();
161
162
        return $node;
163
    }
164
}
165