InneairTransactionExtension::load()   C
last analyzed

Complexity

Conditions 8
Paths 13

Size

Total Lines 46
Code Lines 32

Duplication

Lines 11
Ratio 23.91 %

Code Coverage

Tests 36
CRAP Score 8

Importance

Changes 0
Metric Value
dl 11
loc 46
ccs 36
cts 36
cp 1
rs 5.5555
c 0
b 0
f 0
cc 8
eloc 32
nc 13
nop 2
crap 8
1
<?php
2
3
/**
4
 * Copyright 2016 Inneair
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 * @license http://www.apache.org/licenses/LICENSE-2.0.html Apache-2.0
19
 */
20
21
namespace Inneair\TransactionBundle\DependencyInjection;
22
23
use Exception;
24
use InvalidArgumentException;
25
use ReflectionClass;
26
use ReflectionException;
27
use Inneair\TransactionBundle\Annotation\Transactional;
28
use Symfony\Component\Config\FileLocator;
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
30
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
31
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
32
33
/**
34
 * This class loads and manages the bundle configuration.
35
 */
36
class InneairTransactionExtension extends Extension
37
{
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @throws InvalidArgumentException If the class name of a no rollback exception cannot be found, or it is not a
42
     * valid exception class, or if the configuration options contain an unsupported default policy.
43
     */
44 11
    public function load(array $configs, ContainerBuilder $container)
45
    {
46 11
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
47 11
        $loader->load('services.yml');
48
49 11
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($configs, $container) is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
51 11
        $container->setParameter(
52 11
            Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE,
53 11
            $config[Configuration::STRICT_MODE]
54 11
        );
55
56 11
        switch ($config[Configuration::DEFAULT_POLICY]) {
57 11
            case Configuration::POLICY_NOT_REQUIRED:
58 1
                $policy = Transactional::NOT_REQUIRED;
59 1
                break;
60 10
            case Configuration::POLICY_REQUIRED:
61 6
                $policy = Transactional::REQUIRED;
62 6
                break;
63 4
            case Configuration::POLICY_NESTED:
64 3
                $policy = Transactional::NESTED;
65 3
                break;
66 1
            default:
67 1
                throw new InvalidArgumentException(
68 1
                    'Unsupported default policy "' . $config[Configuration::DEFAULT_POLICY] . '"'
69 1
                );
70 11
        }
71 10
        $container->setParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY, $policy);
72
73 10
        $noRollbackExceptions = array_unique($config[Configuration::NO_ROLLBACK_EXCEPTIONS]);
74 10 View Code Duplication
        foreach ($noRollbackExceptions as $exceptionClassName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            try {
76 4
                $exceptionClass = new ReflectionClass($exceptionClassName);
77 4
            } catch (ReflectionException $e) {
78 1
                throw new InvalidArgumentException('Class not found: \'' . $exceptionClassName . '\'', null, $e);
79
            }
80
81 3
            if (($exceptionClassName !== Exception::class) && !$exceptionClass->isSubclassOf(Exception::class)) {
82 1
                throw new InvalidArgumentException('Not an exception: \'' . $exceptionClassName . '\'');
83
            }
84 8
        }
85 8
        $container->setParameter(
86 8
            Configuration::ROOT_NODE_NAME . '.' . Configuration::NO_ROLLBACK_EXCEPTIONS,
87
            $noRollbackExceptions
88 8
        );
89 8
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94 3
    public function getXsdValidationBasePath()
95
    {
96 3
        return __DIR__ . '/../Resources/config/schema';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __DIR__ . '/../Resources/config/schema'; (string) is incompatible with the return type of the parent method Symfony\Component\Depend...etXsdValidationBasePath of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
97 1
    }
98
}
99