UnknownDocumentTypePanicker::tryHandle()   B
last analyzed

Complexity

Conditions 10
Paths 52

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 0
cts 47
cp 0
rs 7.2242
c 0
b 0
f 0
cc 10
nc 52
nop 3
crap 110

How to fix   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
 * Created by PhpStorm.
4
 * User: peter
5
 * Date: 22.10.18
6
 * Time: 21:28
7
 */
8
9
namespace Maslosoft\Mangan\Helpers;
10
11
use Maslosoft\Mangan\Events\Event;
12
use Maslosoft\Mangan\Events\UnknownDocumentType;
13
use Maslosoft\Mangan\Exceptions\TransformatorException;
14
use function array_key_exists;
15
use function get_class;
16
use function gettype;
17
use function is_object;
18
use function sprintf;
19
20
/**
21
 * This class will try to do something on unknown documents
22
 * or will panic.
23
 *
24
 * @package Maslosoft\Mangan\Helpers
25
 */
26
class UnknownDocumentTypePanicker
27
{
28
	public static function tryHandle(&$data, $parent, $parentField)
29
	{
30
		$className = '';
31
		$handled = false;
32
		if(!empty($parent))
33
		{
34
			$event = new UnknownDocumentType();
35
			$event->setData($data);
36
			$event->parent = $parent;
37
			$event->field = $parentField;
38
39
			$handled = Event::handled($parent, UnknownDocumentType::EventName, $event);
40
			if($handled)
41
			{
42
				$data = $event->getData();
43
				if(empty($data['_class']))
44
				{
45
					$handled = false;
46
				}
47
				else
48
				{
49
					$className = $data['_class'];
50
				}
51
			}
52
		}
53
		if(!$handled)
0 ignored issues
show
Bug Best Practice introduced by
The expression $handled of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
54
		{
55
			$params = [];
56
			if(!empty($parent) && is_object($parent))
57
			{
58
				$params[] = sprintf('on model `%s`', get_class($parent));
59
			}
60
			if(!empty($parentField))
61
			{
62
				$params[] = sprintf('on field `%s`', $parentField);
63
			}
64
65
			if(array_key_exists('data', $data))
66
			{
67
				$params[] = sprintf('got type `%s`', gettype($data['data']));
68
69
				if (is_object($data['data']))
70
				{
71
					$params[] = sprintf('instance of `%s`', get_class($data['data']));
72
				}
73
			}
74
			throw new TransformatorException('Could not determine document type ' . implode(', ', $params));
75
		}
76
		return $className;
77
	}
78
}