Completed
Pull Request — master (#9)
by Tomáš
05:02
created

SculpinCompilerExtension::beforeCompile()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 163
Code Lines 76

Duplication

Lines 21
Ratio 12.88 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
dl 21
loc 163
ccs 0
cts 105
cp 0
rs 2
c 0
b 0
f 0
cc 24
eloc 76
nc 124425
nop 0
crap 600

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\PHP7_Sculpin\DI;
9
10
use Nette\DI\Compiler;
11
use Nette\DI\CompilerExtension;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14
use Symplify\PHP7_Sculpin\DI\Helper\CollectByTypeTrait;
15
16
final class SculpinCompilerExtension extends CompilerExtension
17
{
18
    use CollectByTypeTrait;
19
20
    public function loadConfiguration()
21
    {
22
        Compiler::loadDefinitions(
23
            $this->getContainerBuilder(),
24
            $this->loadFromFile(__DIR__.'/../config/services.neon')
0 ignored issues
show
Bug introduced by
It seems like $this->loadFromFile(__DI.../config/services.neon') targeting Nette\DI\CompilerExtension::loadFromFile() can also be of type string; however, Nette\DI\Compiler::loadDefinitions() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
25
        );
26
    }
27
28
    public function beforeCompile()
29
    {
30
        $containerBuilder = $this->getContainerBuilder();
31
32
        $this->collectServicesByType(
33
            EventDispatcherInterface::class,
34
            EventSubscriberInterface::class,
35
            'addSubscriber'
36
        );
37
38
        //
39
        $definition = $containerBuilder->getDefinition('sculpin_posts.posts_map');
40
        foreach ($containerBuilder->findByTag('sculpin_posts.posts_map') as $id => $tagAttributes) {
41
            foreach ($tagAttributes as $attributes) {
42
                $definition->addSetup('addMap', ['@' . $id]);
43
            }
44
        }
45
46
        $definition = $containerBuilder->getDefinition('sculpin.converter_manager');
47 View Code Duplication
        foreach ($containerBuilder->findByTag('sculpin.converter') as $id => $tagAttributes) {
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...
48
            foreach ($tagAttributes as $attributes) {
49
                $definition->addSetup('registerConverter', [$attributes['alias'], '@' . $id]
50
                );
51
            }
52
        }
53
54
        $definition = $containerBuilder->getDefinition('sculpin.custom_mime_types_repository');
55
        $data = [];
56
        foreach ($containerBuilder->findByTag('sculpin.custom_mime_extensions') as $tagAttributes) {
57
            foreach ($tagAttributes as $attributes) {
58
                $type = $attributes['type'];
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
                $parameter = $attributes['parameter'];
60
61
                dump($parameter);
62
                die;
0 ignored issues
show
Coding Style Compatibility introduced by
The method beforeCompile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
63
64
                if ($containerBuilder->hasParameter($parameter)) {
0 ignored issues
show
Unused Code introduced by
if ($containerBuilder->h...r($parameter)); } } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
65
                    if (isset($data[$type])) {
66
                        $data[$type] = array_unique(array_merge(
67
                            $containerBuilder->getParameter($type),
68
                            $containerBuilder->getParameter($parameter)
69
                        ));
70
                    } else {
71
                        $data[$type] = array_unique($containerBuilder->getParameter($parameter));
72
                    }
73
                }
74
            }
75
        }
76
77
        foreach ($data as $type => $extensions) {
78
            $data[$type] = array_filter($extensions, function ($var) {
79
                return strlen($var) > 0;
80
            });
81
        }
82
83
        $definition->addArgument($data);
84
85
86
        $definition = $containerBuilder->getDefinition('sculpin.formatter_manager');
87 View Code Duplication
        foreach ($containerBuilder->findByTag('sculpin.formatter') as $id => $tagAttributes) {
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...
88
            foreach ($tagAttributes as $attributes) {
89
                $definition->addSetup('registerFormatter', [$attributes['alias'], '@' . $id]);
90
            }
91
        }
92
93
        $definition = $containerBuilder->getDefinition('sculpin.generator_manager');
94 View Code Duplication
        foreach ($containerBuilder->findByTag('sculpin.generator') as $id => $tagAttributes) {
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...
95
            foreach ($tagAttributes as $attributes) {
96
                $definition->addSetup('registerGenerator', [$attributes['alias'], '@' . $id]);
97
            }
98
        }
99
100
        $definition = $containerBuilder->getDefinition('sculpin.data_source');
101
        foreach ($containerBuilder->findByTag('sculpin.data_source') as $id => $tagAttributes) {
102
            $definition->addSetup('addDataSource', ['@' . $id]);
103
        }
104
105
        $definition = $containerBuilder->getDefinition('event_dispatcher');
106
        foreach ($containerBuilder->findByTag('kernel.event_subscriber') as $id => $attributes) {
107
            $class = $containerBuilder->getDefinition($id)->getClass();
108
            $definition->addSetup('addSubscriberService', [$id, $class]);
109
        }
110
111
        // twig
112
        $definition = $containerBuilder->getDefinition('sculpin_twig.twig');
113
114
        // Extensions must always be registered before everything else.
115
        // For instance, global variable definitions must be registered
116
        // afterward. If not, the globals from the extensions will never
117
        // be registered.
118
        $calls = $definition->getMethodCalls();
119
        $definition->setMethodCalls([]);
120
        foreach ($containerBuilder->findByTag('twig.extension') as $id => $attributes) {
121
            $definition->addSetup('addExtension', ['@' . $id]);
122
        }
123
        $definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls));
124
125
126
        // twig loaders
127
        $definition = $containerBuilder->getDefinition('sculpin_twig.loader');
128
        $arguments = $definition->getParameters();
129
        $loaders = $arguments[0];
130
131
        $prependedLoaders = [];
132
        $appendedLoaders = [];
133
        foreach ($containerBuilder->findByTag('twig.loaders.prepend') as $id => $attributes) {
134
            $prependedLoaders[] = '@' . $id;
135
        }
136
137
        foreach ($containerBuilder->findByTag('twig.loaders.append') as $id => $attributes) {
138
            $appendedLoaders[] = '@' . $id;
139
        }
140
141
        $parameters = $containerBuilder->parameters;
142
        $sourceViewPaths = $parameters['sculpin_twig.source_view_paths'];
143
        foreach ($parameters['kernel.bundles'] as $class) {
144
            $reflection = new \ReflectionClass($class);
145
            foreach ($sourceViewPaths as $sourceViewPath) {
146
                if (is_dir($dir = dirname($reflection->getFilename()) . '/Resources/' . $sourceViewPath)) {
147
                    $appendedLoaders[] = $dir;
148
                }
149
            }
150
        }
151
152
        $arguments[0] = array_merge($prependedLoaders, $loaders, $appendedLoaders);
153
        $definition->setArguments($arguments);
154
155
156
        $definition = $containerBuilder->getDefinition('sculpin.data_provider_manager');
157
158 View Code Duplication
        foreach ($containerBuilder->findByTag('sculpin.data_provider') as $id => $tagAttributes) {
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...
159
            foreach ($tagAttributes as $attributes) {
160
                $definition->addSetup('registerDataProvider', [$attributes['alias'], '@' . $id]);
161
            }
162
        }
163
164
///**
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
165
// * {@inheritdoc}
166
// */
167
//public function process(ContainerBuilder $container)
168
//{
169
//    foreach ($container->findByTag('sculpin.path_configurator') as $tagAttributes) {
170
//        foreach ($tagAttributes as $attributes) {
171
//            $typeParameter = 'sculpin.'.$attributes['type'];
172
//            $parameter = $attributes['parameter'];
173
//
174
//            if ($container->hasParameter($parameter)) {
175
//                $value = $container->getParameter($parameter);
176
//                if (!is_array($value)) {
177
//                    $value = [$value];
178
//                }
179
//
180
//                if ($container->hasParameter($typeParameter)) {
181
//                    $container->setParameter($typeParameter, array_unique(array_merge($container->getParameter($typeParameter), $this->antify($value))));
182
//                } else {
183
//                    $container->setParameter($typeParameter, array_unique($this->antify($value)));
184
//                }
185
//            }
186
//        }
187
//    }
188
//}
189
//}
190
    }
191
192
//    private function antify(array $paths) : array
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
193
//    {
194
//        $matcher = new AntPathMatcher();
195
//
196
//        return array_map(
197
//            function ($path) use ($matcher) {
198
//                if ($matcher->isPattern($path)) {
199
//                    return $path;
200
//                }
201
//
202
//                return $path.'/**';
203
//            },
204
//            $paths
205
//        );
206
//    }
207
}
208