DatabaseCompareReal::compare()   F
last analyzed

Complexity

Conditions 12
Paths 1024

Size

Total Lines 59
Code Lines 32

Duplication

Lines 15
Ratio 25.42 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 15
loc 59
rs 3.0929
ccs 0
cts 43
cp 0
cc 12
eloc 32
nc 1024
nop 1
crap 156

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
3
namespace Etobi\CoreAPI\Service;
4
5
/**
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
use InvalidArgumentException;
18
19
/**
20
 * Class DatabaseRealCompare
21
 * 
22
 * @package Etobi\CoreAPI\Service
23
 * @author Georg Ringer <[email protected]>
24
 * @author  Stefano Kowalke <[email protected]>
25
 */
26
class DatabaseCompareReal extends DatabaseComparator {
27
28
	/**
29
	 * Database compare.
30
	 *
31
	 * @param string $actions comma separated list of IDs
32
	 *
33
	 * @throws InvalidArgumentException
34
	 * @return array
35
	 */
36
	public function compare($actions) {
0 ignored issues
show
Complexity introduced by
This operation has 1024 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
37
		$errors = array();
38
		$results = array();
39
40
		$allowedActions = $this->getAllowedActions($actions);
41
42
		$expectedSchema = $this->sqlExpectedSchemaService->getExpectedDatabaseSchema();
43
		$currentSchema = $this->schemaMigrationService->getFieldDefinitions_database();
44
45
		$addCreateChange = $this->schemaMigrationService->getDatabaseExtra($expectedSchema, $currentSchema);
46
		$addCreateChange = $this->schemaMigrationService->getUpdateSuggestions($addCreateChange);
47
48
		$dropRemove = $this->schemaMigrationService->getDatabaseExtra($currentSchema, $expectedSchema);
49
		$dropRemove = $this->schemaMigrationService->getUpdateSuggestions($dropRemove, 'remove');
50
51
		$allowedRequestKeys = $this->getRequestKeys($addCreateChange, $dropRemove);
52
53
		if ($allowedActions[self::ACTION_UPDATE_CLEAR_TABLE] == 1) {
54
			$results[] = $this->schemaMigrationService->performUpdateQueries($addCreateChange['clear_table'], $allowedRequestKeys);
55
		}
56
57
		if ($allowedActions[self::ACTION_UPDATE_ADD] == 1) {
58
			$results[] = $this->schemaMigrationService->performUpdateQueries($addCreateChange['add'], $allowedRequestKeys);
59
		}
60
61 View Code Duplication
		if ($allowedActions[self::ACTION_UPDATE_CHANGE] == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
			$results[] = $this->schemaMigrationService->performUpdateQueries($addCreateChange['change'], $allowedRequestKeys);
63
		}
64
65
		if ($allowedActions[self::ACTION_UPDATE_CREATE_TABLE] == 1) {
66
			$results[] = $this->schemaMigrationService->performUpdateQueries($addCreateChange['create_table'], $allowedRequestKeys);
67
		}
68
69 View Code Duplication
		if ($allowedActions[self::ACTION_REMOVE_CHANGE] == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
			$results[] = $this->schemaMigrationService->performUpdateQueries($dropRemove['change'], $allowedRequestKeys);
71
		}
72
73 View Code Duplication
		if ($allowedActions[self::ACTION_REMOVE_DROP] == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
74
			$results[] = $this->schemaMigrationService->performUpdateQueries($dropRemove['drop'], $allowedRequestKeys);
75
		}
76
77 View Code Duplication
		if ($allowedActions[self::ACTION_REMOVE_CHANGE_TABLE] == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
78
			$results[] = $this->schemaMigrationService->performUpdateQueries($dropRemove['change_table'], $allowedRequestKeys);
79
		}
80
81 View Code Duplication
		if ($allowedActions[self::ACTION_REMOVE_DROP_TABLE] == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
82
			$results[] = $this->schemaMigrationService->performUpdateQueries($dropRemove['drop_table'], $allowedRequestKeys);
83
		}
84
85
		foreach ($results as $resultSet) {
86
			if (is_array($resultSet)) {
87
				foreach ($resultSet as $key => $errorMessage) {
88
					$errors[$key] = $errorMessage;
89
				}
90
			}
91
		}
92
93
		return $errors;
94
	}
95
96
	/**
97
	 * Get all request keys, even for those requests which are not used.
98
	 *
99
	 * @param array $update
100
	 * @param array $remove
101
	 *
102
	 * @return array
103
	 */
104
	protected function getRequestKeys(array $update, array $remove) {
105
		$tmpKeys = array();
106
107
		$updateActions = array('clear_table', 'add', 'change', 'create_table');
108
		$removeActions = array('change', 'drop', 'change_table', 'drop_table');
109
110 View Code Duplication
		foreach ($updateActions as $updateAction) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
111
			if (isset($update[$updateAction]) && is_array($update[$updateAction])) {
112
				$tmpKeys[] = array_keys($update[$updateAction]);
113
			}
114
		}
115
116 View Code Duplication
		foreach ($removeActions as $removeAction) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
117
			if (isset($remove[$removeAction]) && is_array($remove[$removeAction])) {
118
				$tmpKeys[] = array_keys($remove[$removeAction]);
119
			}
120
		}
121
122
		$finalKeys = array();
123
		foreach ($tmpKeys as $keys) {
124
			foreach ($keys as $key) {
125
				$finalKeys[$key] = TRUE;
126
			}
127
		}
128
		return $finalKeys;
129
	}
130
}
131
132