Convert   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 65
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 9 1
A execute() 0 20 4
A loadConfigurations() 0 12 1
1
<?php
2
namespace GoetasWebservices\Xsd\XsdToPhp\Command;
3
4
use Symfony\Component\Config\FileLocator;
5
use Symfony\Component\Config\Loader\DelegatingLoader;
6
use Symfony\Component\Config\Loader\LoaderResolver;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
13
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14
15
class Convert extends Command
16
{
17
    /**
18
     * @var ContainerInterface
19
     */
20
    protected $container;
21
22
    public function __construct(ContainerInterface $container)
23
    {
24
        $this->container = $container;
25
        parent::__construct();
26
    }
27
28
    /**
29
     *
30
     * @see Console\Command\Command
31
     */
32
    protected function configure()
33
    {
34
        $this->setName('convert');
35
        $this->setDescription("Convert a XSD file into PHP classes and JMS serializer metadata files");
36
        $this->setDefinition(array(
37
            new InputArgument('config', InputArgument::REQUIRED, 'Where is located your XSD definitions'),
38
            new InputArgument('src', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Where is located your XSD definitions'),
39
        ));
40
    }
41
42
    /**
43
     *
44
     * @see Console\Command\Command
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $this->loadConfigurations($input->getArgument('config'));
49
        $src = $input->getArgument('src');
50
51
        $schemas = [];
52
        $reader = $this->container->get('goetas_webservices.xsd2php.schema_reader');
53
        foreach ($src as $file) {
54
            $schemas[] = $reader->readFile($file);
55
        }
56
57
        foreach (['php', 'jms'] as $type) {
58
            $converter = $this->container->get('goetas_webservices.xsd2php.converter.' . $type);
59
            $items = $converter->convert($schemas);
60
61
            $writer = $this->container->get('goetas_webservices.xsd2php.writer.' . $type);
62
            $writer->write($items);
63
        }
64
        return count($items) ? 0 : 255;
0 ignored issues
show
Bug introduced by
The variable $items does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
65
    }
66
67
    protected function loadConfigurations($configFile)
68
    {
69
        $locator = new FileLocator('.');
70
        $yaml = new YamlFileLoader($this->container, $locator);
0 ignored issues
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ction\ContainerBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
71
        $xml = new XmlFileLoader($this->container, $locator);
0 ignored issues
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ction\ContainerBuilder>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
72
73
        $delegatingLoader = new DelegatingLoader(new LoaderResolver(array($yaml, $xml)));
74
        $delegatingLoader->load($configFile);
75
76
        $this->container->compile();
77
78
    }
79
}
80