Completed
Push — master ( 09ac4b...4b8ea8 )
by Jáchym
12s
created

MigrationsExtension::keepBcForDirsOption()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
ccs 3
cts 5
cp 0.6
crap 3.576
1
<?php
2
3
/**
4
 * This file is part of Zenify
5
 * Copyright (c) 2014 Tomas Votruba (http://tomasvotruba.cz)
6
 */
7
8
namespace Zenify\DoctrineMigrations\DI;
9
10
use Arachne\EventDispatcher\DI\EventDispatcherExtension;
11
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand;
12
use Nette\DI\CompilerExtension;
13
use Symfony\Component\Console\Application;
14
use Zenify\DoctrineMigrations\CodeStyle\CodeStyle;
15
use Zenify\DoctrineMigrations\Configuration\Configuration;
16
use Zenify\DoctrineMigrations\EventSubscriber\ChangeCodingStandardEventSubscriber;
17
use Zenify\DoctrineMigrations\EventSubscriber\RegisterMigrationsEventSubscriber;
18
use Zenify\DoctrineMigrations\EventSubscriber\SetConsoleOutputEventSubscriber;
19
use Zenify\DoctrineMigrations\Exception\DI\MissingExtensionException;
20
21
22
final class MigrationsExtension extends CompilerExtension
23
{
24
25
	/**
26
	 * @var string[]
27
	 */
28
	private $defaults = [
29
		'table' => 'doctrine_migrations',
30
		'column' => 'version',
31
		'directory' => '%appDir%/../migrations',
32
		'namespace' => 'Migrations',
33
		'codingStandard' => CodeStyle::INDENTATION_TABS,
34
		'versionsOrganization' => NULL,
35
	];
36
37
	/**
38
	 * @var string[]
39
	 */
40
	private $subscribers = [
41
		ChangeCodingStandardEventSubscriber::class,
42
		RegisterMigrationsEventSubscriber::class,
43
		SetConsoleOutputEventSubscriber::class,
44
	];
45
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50 6
	public function loadConfiguration()
51
	{
52 6
		$this->ensureEventDispatcherExtensionIsRegistered();
53
54 5
		$containerBuilder = $this->getContainerBuilder();
55
56 5
		$this->compiler->parseServices(
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\Compiler::parseServices() has been deprecated.

This method has been deprecated.

Loading history...
57 5
			$containerBuilder,
58 5
			$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::parseServices() 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...
59 5
		);
60
61 5
		foreach ($this->subscribers as $key => $subscriber) {
62
			$definition = $containerBuilder
0 ignored issues
show
Unused Code introduced by
$definition 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...
63 5
				->addDefinition($this->prefix('listener' . $key))
64 5
				->setClass($subscriber)
65 5
				->addTag(EventDispatcherExtension::TAG_SUBSCRIBER);
66 5
		}
67
68 5
		$config = $this->getValidatedConfig();
69
70 5
		$containerBuilder->addDefinition($this->prefix('codeStyle'))
71 5
			->setClass(CodeStyle::class)
72 5
			->setArguments([$config['codingStandard']]);
73
74 5
		$this->addConfigurationDefinition($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->getValidatedConfig() on line 68 can also be of type string; however, Zenify\DoctrineMigration...nfigurationDefinition() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75 5
	}
76
77
78
	/**
79
	 * {@inheritdoc}
80
	 */
81 4
	public function beforeCompile()
82
	{
83 4
		$containerBuilder = $this->getContainerBuilder();
84 4
		$containerBuilder->prepareClassList();
85
86 4
		$this->setConfigurationToCommands();
87 4
		$this->loadCommandsToApplication();
88 4
	}
89
90
91 5
	private function addConfigurationDefinition(array $config)
92
	{
93 5
		$containerBuilder = $this->getContainerBuilder();
94 5
		$configurationDefinition = $containerBuilder->addDefinition($this->prefix('configuration'));
95
		$configurationDefinition
96 5
			->setClass(Configuration::class)
97 5
			->addSetup('setMigrationsTableName', [$config['table']])
98 5
			->addSetup('setMigrationsColumnName', [$config['column']])
99 5
			->addSetup('setMigrationsDirectory', [$config['directory']])
100 5
			->addSetup('setMigrationsNamespace', [$config['namespace']]);
101
102 5
		if ($config['versionsOrganization'] === Configuration::VERSIONS_ORGANIZATION_BY_YEAR) {
103
			$configurationDefinition->addSetup('setMigrationsAreOrganizedByYear');
104
105 5
		} elseif ($config['versionsOrganization'] === Configuration::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) {
106
			$configurationDefinition->addSetup('setMigrationsAreOrganizedByYearAndMonth');
107
		}
108 5
	}
109
110
111 4 View Code Duplication
	private function setConfigurationToCommands()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
112
	{
113 4
		$containerBuilder = $this->getContainerBuilder();
114 4
		$configurationDefinition = $containerBuilder->getDefinition($containerBuilder->getByType(Configuration::class));
115
116 4
		foreach ($containerBuilder->findByType(AbstractCommand::class) as $commandDefinition) {
117 4
			$commandDefinition->addSetup('setMigrationConfiguration', ['@' . $configurationDefinition->getClass()]);
118 4
		}
119 4
	}
120
121
122 4 View Code Duplication
	private function loadCommandsToApplication()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
123
	{
124 4
		$containerBuilder = $this->getContainerBuilder();
125 4
		$applicationDefinition = $containerBuilder->getDefinition($containerBuilder->getByType(Application::class));
126 4
		foreach ($containerBuilder->findByType(AbstractCommand::class) as $name => $commandDefinition) {
127 4
			$applicationDefinition->addSetup('add', ['@' . $name]);
128 4
		}
129 4
	}
130
131
132
	/**
133
	 * @return array
134
	 */
135 5
	private function getValidatedConfig()
136
	{
137 5
		$configuration = $this->getConfig($this->defaults);
138 5
		$this->validateConfig($configuration);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfig($this->defaults) on line 137 can also be of type string; however, Nette\DI\CompilerExtension::validateConfig() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
139 5
		$configuration['directory'] = $this->getContainerBuilder()->expand($configuration['directory']);
0 ignored issues
show
Deprecated Code introduced by
The method Nette\DI\ContainerBuilder::expand() has been deprecated.

This method has been deprecated.

Loading history...
140
141 5
		return $configuration;
142
	}
143
144
145 6
	private function ensureEventDispatcherExtensionIsRegistered()
146
	{
147 6
		if ( ! $this->compiler->getExtensions(EventDispatcherExtension::class)) {
148 1
			throw new MissingExtensionException(
149 1
				sprintf('Please register required extension "%s" to your config.', EventDispatcherExtension::class)
150 1
			);
151
		}
152 5
	}
153
154
}
155