Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

Command/GenerateDefaultPagePartsCommand.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Command;
4
5
use Kunstmaan\GeneratorBundle\Generator\DefaultPagePartGenerator;
6
use Symfony\Component\Console\Input\InputOption;
7
8
/**
9
 * Generates the default pageparts
10
 */
11
class GenerateDefaultPagePartsCommand extends KunstmaanGenerateCommand
12
{
13
    /**
14
     * @var BundleInterface
15
     */
16
    private $bundle;
17
18
    /**
19
     * @var string
20
     */
21
    private $prefix;
22
23
    /**
24
     * @var array
25
     */
26
    private $sections;
27
28
    /**
29
     * @var bool
30
     */
31
    private $behatTest;
32
33
    /**
34
     * @see Command
35
     */
36
    protected function configure()
37
    {
38
        $this->setDescription('Generates default pageparts')
39
            ->setHelp(<<<'EOT'
40
The <info>kuma:generate:default-pageparts</info> command generates the default pageparts and adds the pageparts configuration.
41
42
<info>php bin/console kuma:generate:default-pageparts</info>
43
EOT
44
            )
45
            ->addOption('namespace', '', InputOption::VALUE_OPTIONAL, 'The namespace to generate the default pageparts in')
46
            ->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table name of the generated entity')
47
            ->addOption('contexts', '', InputOption::VALUE_OPTIONAL, 'The contexts were we need to allow the pageparts (separated multiple sections with a comma)')
48
            ->setName('kuma:generate:default-pageparts');
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function getWelcomeText()
55
    {
56
        return 'Welcome to the Kunstmaan default pageparts generator';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function doExecute()
63
    {
64
        $this->assistant->writeSection('Default PageParts generation');
65
66
        $pagepartNames = array(
67
            'AbstractPagePart',
68
            'AudioPagePart',
69
            'ButtonPagePart',
70
            'DownloadPagePart',
71
            'HeaderPagePart',
72
            'ImagePagePart',
73
            'IntroTextPagePart',
74
            'LinePagePart',
75
            'LinkPagePart',
76
            'RawHtmlPagePart',
77
            'TextPagePart',
78
            'TocPagePart',
79
            'ToTopPagePart',
80
            'VideoPagePart',
81
        );
82
83
        foreach ($pagepartNames as $pagepartName) {
84
            $this->createGenerator()->generate($this->bundle, $pagepartName, $this->prefix, $this->sections, $this->behatTest);
85
        }
86
87
        $this->assistant->writeSection('PageParts successfully created', 'bg=green;fg=black');
88
        $this->assistant->writeLine(array(
89
                'Make sure you update your database first before you test the pagepart:',
90
                '    Directly update your database:          <comment>bin/console doctrine:schema:update --force</comment>',
91
                '    Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && bin/console doctrine:migrations:migrate</comment>', )
92
        );
93
94
        return 0;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function doInteract()
101
    {
102
        if (!$this->isBundleAvailable('KunstmaanPagePartBundle')) {
103
            $this->assistant->writeError('KunstmaanPagePartBundle not found', true);
104
        }
105
106
        $this->assistant->writeLine(array("This command helps you to generate a new pagepart.\n"));
107
108
        /**
109
         * Ask for which bundle we need to create the pagepart
110
         */
111
        $bundleNamespace = $this->assistant->getOptionOrDefault('namespace', null);
112
        $this->bundle = $this->askForBundleName('pagepart', $bundleNamespace);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->askForBundleName(...art', $bundleNamespace) of type object<Symfony\Component...Bundle\BundleInterface> is incompatible with the declared type object<Kunstmaan\Generat...ommand\BundleInterface> of property $bundle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
113
114
        /*
115
         * Ask the database table prefix
116
         */
117
        $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
118
119
        /**
120
         * Ask for which page sections we should enable this pagepart
121
         */
122
        $contexts = $this->assistant->getOptionOrDefault('contexts', null);
123
        if ($contexts) {
124
            $contexts = explode(',', $contexts);
125
            array_walk($contexts, 'trim');
126
127
            $this->sections = array();
128
            $allSections = $this->getAvailableSections($this->bundle);
129
            foreach ($allSections as $section) {
130
                if (in_array($section['context'], $contexts)) {
131
                    $this->sections[] = $section['file'];
132
                }
133
            }
134
        } else {
135
            $question = 'In which page section configuration file(s) do you want to add the pageparts (multiple possible, separated by comma)';
136
            $this->sections = $this->askForSections($question, $this->bundle, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->askForSections($q...n, $this->bundle, true) can be null. However, the property $sections is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
137
        }
138
139
        /*
140
         * Check that we can create behat tests for the default pagepart
141
         */
142
        $this->behatTest = (count($this->sections) > 0 && $this->canGenerateBehatTests($this->bundle));
143
    }
144
145
    /**
146
     * Get the generator.
147
     *
148
     * @return DefaultPagePartGenerator
149
     */
150 View Code Duplication
    protected function createGenerator()
151
    {
152
        $filesystem = $this->getContainer()->get('filesystem');
153
        $registry = $this->getContainer()->get('doctrine');
154
155
        return new DefaultPagePartGenerator($filesystem, $registry, '/pagepart', $this->assistant, $this->getContainer());
0 ignored issues
show
$filesystem is of type object|null, but the function expects a object<Symfony\Component\Filesystem\Filesystem>.

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...
$registry is of type object|null, but the function expects a object<Symfony\Bridge\Doctrine\RegistryInterface>.

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...
156
    }
157
}
158