GenerateFixtureCommand::getYAMLValue()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
namespace Vivait\BootstrapBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Yaml\Yaml;
11
12
class GenerateFixtureCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
13
{
14
    protected function configure()
15
    {
16
        $this
17
          ->setName('generate:fixture')
18
          ->setDescription('Generates a fixture')
19
          ->addArgument('query', InputArgument::REQUIRED, 'What query do you want to create a fixture for?')
20
          ->addOption('chain', null, InputOption::VALUE_NONE, 'If set, it will chain the method calls');
21
    }
22
23
    protected function execute(InputInterface $input, OutputInterface $output)
24
    {
25
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
26
27
        $query = $em->createQuery($input->getArgument('query'));
28
        $chain = $input->getOption('chain');
0 ignored issues
show
Unused Code introduced by
$chain 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...
29
        $code  = '';
30
31
        foreach ($query->getResult() as $result) {
32
            $code .= $this->generateAliceFixture($result);
33
        }
34
35
        file_put_contents($this->getContainer()->getParameter("kernel.cache_dir") . '/fixtures.yml', $code);
36
37
        $output->writeln($code);
38
    }
39
40
    protected function generateFixture($object, $chain = false)
41
    {
42
        $reflection = new \ReflectionObject($object);
43
44
        $output = sprintf('$class = new \%s;%s', $reflection->getName(), PHP_EOL);
0 ignored issues
show
Bug introduced by
Consider using $reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
45
46
        $is_first = true;
47
48
        foreach ($reflection->getMethods() as $method) {
49
            if (substr_compare($method->getName(), 'set', 0, 3) === 0) {
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
50
                $cut_name = substr($method->getName(), 3);
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
51
52
                try {
53
                    $getter = $reflection->getMethod('get' . $cut_name);
54
                } catch (\ReflectionException $e) {
55
                    continue;
56
                }
57
58
                // There's a getter
59
                if ($getter) {
60
                    $value = $getter->invoke($object);
61
62
                    if ($value === null || $value === '') {
63
                        continue;
64
                    }
65
66
                    if (is_object($value)) {
67
                        $value_reflection = new \ReflectionObject($value);
68
69
                        try {
70
                            $value_reflection->getMethod('__set_state');
71
                        } catch (\ReflectionException $e) {
72
                            continue;
73
                        }
74
                    }
75
76
                    $output .= sprintf(
77
                      '%s->%s(%s)%s',
78
                      (($chain && !$is_first) ? "\t" : '$class'),
79
                      $method->getName(),
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
80
                      var_export($value, true),
81
                      ($chain ? '' : ';') . PHP_EOL
82
                    );
83
                    $is_first = false;
84
                }
85
            }
86
        }
87
88
        if ($chain) {
89
            $output = substr($output, 0, strlen($output) - 1) . ';' . PHP_EOL;
90
        }
91
92
        $output .= '$manager->persist($class);' . PHP_EOL . PHP_EOL;
93
94
        return $output;
95
    }
96
97
    protected function generateAliceFixture($object, $name = 'district')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
98
    {
99
        $reflection = new \ReflectionObject($object);
100
        $array      = [];
101
102
        $entity = $reflection->getName();
0 ignored issues
show
Bug introduced by
Consider using $reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
103
104
        $key = $reflection->getProperty($name);
105
        $key->setAccessible(true);
106
        $key = $key->getValue($object);
107
108
        foreach ($reflection->getProperties() as $property) {
109
            if (in_array($property->getName(), ['id'])) {
110
                continue;
111
            }
112
113
            $property->setAccessible(true);
114
            $value = $property->getValue($object);
115
            $value = $this->getYAMLValue($value);
116
117
            if ($value === null) {
118
                continue;
119
            }
120
121
            $array[(string)$key][$property->getName()] = $value;
122
        }
123
124
        return Yaml::dump([$entity => $array], 3);
125
    }
126
127
    protected function getYAMLValue($value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
128
    {
129
        if (is_array($value) || $value instanceOf \Traversable) {
130
            $new_value = [];
131
            foreach ($value as $foreign) {
132
                // TODO: Make this more flexible
133
                $new_value[] = $this->getYAMLValue($foreign);
134
            }
135
            return $new_value;
136
        } else if (is_object($value)) {
137
            return '@' . $value->getId();
138
        } else {
139
            return $value;
140
        }
141
    }
142
}
143