Completed
Pull Request — master (#9)
by Asmir
231:28 queued 229:43
created

SoapClientExtension::load()   C

Complexity

Conditions 10
Paths 168

Size

Total Lines 78
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 10.0007

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 50
cts 51
cp 0.9804
rs 5.3146
c 0
b 0
f 0
cc 10
eloc 42
nc 168
nop 2
crap 10.0007

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
namespace GoetasWebservices\SoapServices\SoapClient\DependencyInjection;
3
4
use Psr\Log\NullLogger;
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Extension\Extension;
9
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
10
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
11
12
class SoapClientExtension extends Extension implements PrependExtensionInterface
13
{
14 2
    public function load(array $configs, ContainerBuilder $container)
15
    {
16 2
        $config = $this->processConfiguration(new Configuration(), $configs);
17 2
        foreach ($configs as $subConfig) {
18 2
            $config = array_merge($config, $subConfig);
19 2
        }
20
21 2
        $container->setParameter('goetas_webservices.soap_client.config', $config);
22 2
        $container->setParameter('goetas_webservices.soap_client.unwrap_returns', $config['unwrap_returns']);
23
24 2
        $xml = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
25 2
        $xml->load('services.xml');
26
27
28 2
        $container->setDefinition('logger', new Definition(NullLogger::class));
29
30 2
        $definition = $container->getDefinition('goetas_webservices.xsd2php.path_generator.jms.' . $config['path_generator']);
31 2
        $container->setDefinition('goetas_webservices.xsd2php.path_generator.jms', clone $definition);
32
33 2
        $definition = $container->getDefinition('goetas_webservices.xsd2php.path_generator.php.' . $config['path_generator']);
34 2
        $container->setDefinition('goetas_webservices.xsd2php.path_generator.php', clone $definition);
35
36
37 2
        $pathGenerator = $container->getDefinition('goetas_webservices.xsd2php.path_generator.jms');
38 2
        $pathGenerator->addMethodCall('setTargets', [$config['destinations_jms']]);
39
40 2
        $pathGenerator = $container->getDefinition('goetas_webservices.xsd2php.path_generator.php');
41 2
        $pathGenerator->addMethodCall('setTargets', [$config['destinations_php']]);
42
43
44 2
        foreach (['php', 'jms'] as $type) {
45
46 2
            $converter = $container->getDefinition('goetas_webservices.xsd2php.converter.' . $type);
47
48 2
            $converterWsdl = $container->getDefinition('goetas_webservices.wsdl2php.converter.' . $type);
0 ignored issues
show
Unused Code introduced by
$converterWsdl 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...
49
50 2
            foreach ($config['namespaces'] as $xml => $php) {
51 2
                $converter->addMethodCall('addNamespace', [$xml, self::sanitizePhp($php)]);
52 2
            }
53 2
            foreach ($config['aliases'] as $xml => $data) {
54 2
                foreach ($data as $type => $php) {
55 2
                    $converter->addMethodCall('addAliasMapType', [$xml, $type, self::sanitizePhp($php)]);
56 2
                }
57 2
            }
58 2
        }
59
60 2
        $schemaReader = $container->getDefinition('goetas_webservices.xsd2php.schema_reader');
61 2
        foreach ($config['known_locations'] as $namespace => $location) {
62
            $schemaReader->addMethodCall('addKnownSchemaLocation', [$namespace, $location]);
63 2
        }
64
65
66 2
        $definition = $container->getDefinition('goetas_webservices.xsd2php.naming_convention.' . $config['naming_strategy']);
67 2
        $container->setDefinition('goetas_webservices.xsd2php.naming_convention', $definition);
68
69
//////////
70
71 2
        $metadataGenerator = $container->getDefinition('goetas_webservices.soap_client.metadata.generator');
72 2
        foreach ($config['alternative_endpoints'] as $service => $data) {
73 2
            foreach ($data as $port => $endPoint) {
74 2
                $metadataGenerator->addMethodCall('addAlternativeEndpoint', [$service, $port, $endPoint]);
75 2
            }
76 2
        }
77 2
        $keys = ['headers', 'parts', 'messages'];
0 ignored issues
show
Unused Code introduced by
$keys 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...
78
        //$metadataGenerator->addMethodCall('setBaseNs', [array_intersect_key($config, array_combine($keys, $keys))]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
79 2
        $metadataGenerator->addMethodCall('setUnwrap', [$config['unwrap_returns']]);
80 2
        $metadataGenerator->replaceArgument(1, $config['namespaces']);
81
82 2
        $writer = $container->getDefinition('goetas_webservices.soap_client.stub.client_generator');
83 2
        $writer->addMethodCall('setUnwrap', [$config['unwrap_returns']]);
84
85
86 2
        $forProduction = !!$container->getParameter('goetas_webservices.soap_client.metadata');
87
88 2
        $readerName = 'goetas_webservices.soap_client.metadata_loader.' . ($forProduction ? 'array' : 'dev');
89 2
        $container->setAlias('goetas_webservices.soap_client.metadata_reader', $readerName);
90
91 2
    }
92
93 2
    protected static function sanitizePhp($ns)
94
    {
95 2
        return strtr($ns, '/', '\\');
96
    }
97
98 2
    public function getAlias()
99
    {
100 2
        return 'soap_client';
101
    }
102
103
    /**
104
     * Allow an extension to prepend the extension configurations.
105
     *
106
     * @param ContainerBuilder $container
107
     */
108 2
    public function prepend(ContainerBuilder $container)
109
    {
110 2
        $container->prependExtensionConfig('goetas_soap_client', []);
111 2
    }
112
}
113