convertFireflyGetsToFiles()   F
last analyzed

Complexity

Conditions 21
Paths 2209

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 462

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 58
rs 0
c 0
b 0
f 0
cc 21
nc 2209
nop 2
ccs 0
cts 41
cp 0
crap 462

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
use yii\db\Migration;
4
5
/**
6
 * Class m190204_134239_firefly_interface_change_db_fix
7
 * Converts old API firefly action links to new api links.
8
 * These become hardcoded when adding images to wysiwyg
9
 */
10
class m190204_134239_firefly_interface_change_db_fix extends Migration
11
{
12
	/**
13
	 * This is the old api url that will no longer exist
14
	 * @var string
15
	 */
16
	private $upSearch = '/firefly/get/img';
17
	/**
18
	 * This is the new api link to convert to
19
	 * @var string
20
	 */
21
	private $upReplace = '/firefly/file/img';
22
23
	public function safeUp()
24
	{
25
		return $this->convertFireflyGetsToFiles($this->upSearch, $this->upReplace);
26
	}
27
28
	public function safeDown()
29
	{
30
		return $this->convertFireflyGetsToFiles($this->upReplace, $this->upSearch);
31
	}
32
33
	private function convertFireflyGetsToFiles($search, $replace)
34
	{
35
		$potentiallyAffected = $this->db->createCommand("SELECT `class_type`, `member_ref` FROM `dds_member` WHERE `data_type_ref`='textlong'")->queryAll();
36
		if (count($potentiallyAffected)==0)
37
			return;
38
		$classes = [];
39
		foreach ($potentiallyAffected as $pa)
40
			$classes[$pa['class_type']][] = $pa['member_ref'];
41
		$phoebeClasses = array_keys($classes);
42
		$phoebe = neon('phoebe')->getIPhoebeType('daedalus');
43
44
		$phoebeDefinition = [];
45
		foreach ($phoebeClasses as $pc)
46
			$phoebeDefinition[$pc] = $phoebe->getClass($pc, null, false);
47
48
		$potentiallyAffectedLookup = [];
49
		foreach ($potentiallyAffected as $pa) {
50
			if (empty($potentiallyAffectedLookup[$pa['class_type']]))
51
				$potentiallyAffectedLookup[$pa['class_type']] = [];
52
			$potentiallyAffectedLookup[$pa['class_type']][] = $pa['member_ref'];
53
		}
54
55
		$wysiwygFields = [];
56
		foreach ($phoebeDefinition as $pdKey => $pd) {
57
			$fields = !empty($pd->definition['fields']) ? $pd->definition['fields'] : null;
58
			if ($fields) {
59
				$potentials = !empty($potentiallyAffectedLookup[$pdKey]) ? $potentiallyAffectedLookup[$pdKey] : [];
60
				foreach ($fields as $field) {
61
					if (in_array($field['name'], $potentials)) {
62
						if ($field['definition']['class'] == "neon\\core\\form\\fields\\Wysiwyg") {
63
							$wysiwygFields[$pdKey] = !empty($wysiwygFields[$pdKey]) ? $wysiwygFields[$pdKey] : [];
64
							$wysiwygFields[$pdKey][] = $field['name'];
65
						}
66
					}
67
				}
68
			}
69
		}
70
71
		$dds = neon('dds')->IDdsObjectManagement;
0 ignored issues
show
Bug Best Practice introduced by
The property IDdsObjectManagement does not exist on neon\core\ApplicationWeb. Since you implemented __get, consider adding a @property annotation.
Loading history...
72
		foreach ($wysiwygFields as $classType => $fields) {
73
			$start=0;
74
			do {
75
				$found = $dds->listObjects($classType, $total, true, true, $start, 100);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $total seems to be never defined.
Loading history...
Bug introduced by
The method listObjects() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
				/** @scrutinizer ignore-call */ 
76
    $found = $dds->listObjects($classType, $total, true, true, $start, 100);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
				$start += count($found);
77
				foreach ($found as $objectData) {
78
					$changes = [];
79
					foreach ($fields as $field) {
80
						if (!empty($objectData[$field]) && strpos($objectData[$field],$search)!==false)
81
							$changes[$field] = str_replace($search, $replace, $objectData[$field]);
82
					}
83
					if (count($changes)) {
84
						$dds->editObject($objectData['_uuid'], $changes);
85
					}
86
				}
87
			} while (count($found)>0);
88
		}
89
90
		return true;
91
	}
92
93
}
94