Passed
Push — master ( ba7917...6c027d )
by Jean-Christophe
06:26
created

ModelParser::__toString()   C

Complexity

Conditions 13
Paths 144

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 20.3331

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 52
ccs 24
cts 37
cp 0.6485
rs 6.25
c 0
b 0
f 0
cc 13
nc 144
nop 0
crap 20.3331

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 Ubiquity\orm\parser;
4
5
use Ubiquity\utils\base\UArray;
6
use Ubiquity\contents\transformation\TransformersManager;
7
use Ubiquity\exceptions\TransformerException;
8
9
10
/**
11
 * Parse model annotation for cache generation.
12
 * Ubiquity\orm\parser$ModelParser
13
 * This class is part of Ubiquity
14
 *
15
 * @author jcheron <[email protected]>
16
 * @version 1.0.1
17
 *
18
 */
19
class ModelParser {
20
	protected $global;
21
	protected $primaryKeys;
22
	protected $manytoOneMembers;
23
	protected $oneToManyMembers;
24
	protected $manyToManyMembers;
25
	protected $joinColumnMembers;
26
	protected $joinTableMembers;
27
	protected $nullableMembers = [ ];
28
	protected $notSerializableMembers = [ ];
29
	protected $fieldNames;
30
	protected $fieldTypes = [ ];
31
	protected $transformers = [ ];
32
	protected $accessors = [ ];
33
	protected $yuml;
34
35 6
	public function parse($modelClass) {
36 6
		$instance = new $modelClass ();
37 6
		$this->primaryKeys = Reflexion::getKeyFields ( $instance );
38 6
		$this->oneToManyMembers = Reflexion::getMembersAnnotationWithAnnotation ( $modelClass, "@oneToMany" );
39 6
		$this->manytoOneMembers = Reflexion::getMembersNameWithAnnotation ( $modelClass, "@manyToOne" );
40 6
		$this->manyToManyMembers = Reflexion::getMembersAnnotationWithAnnotation ( $modelClass, "@manyToMany" );
41 6
		$this->joinColumnMembers = Reflexion::getMembersAnnotationWithAnnotation ( $modelClass, "@joinColumn" );
42 6
		$this->joinTableMembers = Reflexion::getMembersAnnotationWithAnnotation ( $modelClass, "@joinTable" );
43 6
		$this->transformers = Reflexion::getMembersAnnotationWithAnnotation ( $modelClass, "@transformer" );
44 6
		$yuml = Reflexion::getAnnotationClass ( $modelClass, "@yuml" );
45 6
		if (\sizeof ( $yuml ) > 0)
46
			$this->yuml = $yuml [0];
47 6
		$properties = Reflexion::getProperties ( $instance );
48 6
		foreach ( $properties as $property ) {
49 6
			$propName = $property->getName ();
50 6
			$fieldName = Reflexion::getFieldName ( $modelClass, $propName );
51 6
			$this->fieldNames [$propName] = $fieldName;
52 6
			$nullable = Reflexion::isNullable ( $modelClass, $propName );
53 6
			$serializable = Reflexion::isSerializable ( $modelClass, $propName );
54 6
			if ($nullable)
55 5
				$this->nullableMembers [] = $propName;
56 6
			if (! $serializable)
57 6
				$this->notSerializableMembers [] = $propName;
58 6
			$type = Reflexion::getDbType ( $modelClass, $propName );
59 6
			if ($type === false)
60 5
				$type = "mixed";
61 6
			$this->fieldTypes [$propName] = $type;
62 6
			$accesseur = "set" . ucfirst ( $propName );
63 6
			if (! isset ( $this->accessors [$fieldName] ) && method_exists ( $modelClass, $accesseur )) {
64 6
				$this->accessors [$fieldName] = $accesseur;
65
			}
66
		}
67
68 6
		$this->global ["#tableName"] = Reflexion::getTableName ( $modelClass );
69 6
	}
70
71 6
	public function __toString() {
72 6
		$result = $this->global;
73 6
		$result ["#primaryKeys"] = $this->primaryKeys;
74 6
		$result ["#manyToOne"] = $this->manytoOneMembers;
75 6
		$result ["#fieldNames"] = $this->fieldNames;
76 6
		$result ["#fieldTypes"] = $this->fieldTypes;
77 6
		$result ["#nullable"] = $this->nullableMembers;
78 6
		$result ["#notSerializable"] = $this->notSerializableMembers;
79 6
		$result ["#transformers"] = [ ];
80 6
		$result ["#accessors"] = $this->accessors;
81 6
		if (isset ( $this->yuml ))
82
			$result ["#yuml"] = $this->yuml->getPropertiesAndValues ();
83 6
		foreach ( $this->oneToManyMembers as $member => $annotation ) {
84 5
			$result ["#oneToMany"] [$member] = $annotation->getPropertiesAndValues ();
85
		}
86 6
		foreach ( $this->manyToManyMembers as $member => $annotation ) {
87 5
			$result ["#manyToMany"] [$member] = $annotation->getPropertiesAndValues ();
88
		}
89
90 6
		foreach ( $this->joinTableMembers as $member => $annotation ) {
91 5
			$result ["#joinTable"] [$member] = $annotation->getPropertiesAndValues ();
92
		}
93
94 6
		if (class_exists ( "Ubiquity\\contents\\transformation\\TransformersManager" )) {
95 6
			TransformersManager::start();
96 6
			foreach ( $this->transformers as $member => $annotation ) {
97
				$goodTransformer = false;
98
				if (array_search ( $member, $this->notSerializableMembers, false ) !== false) {
99
					throw new TransformerException ( sprintf ( '%s member is not serializable and does not supports transformers!', $member ) );
100
				}
101
				$trans = TransformersManager::getTransformerClass ( $annotation->name );
102
				if ($trans == null) {
103
					throw new TransformerException ( sprintf ( '%s value is not a declared transformer.', $annotation->name ) );
104
				} else {
105
					foreach ( TransformersManager::TRANSFORMER_TYPES as $transType => $transInterface ) {
106
						if (is_subclass_of ( $trans, $transInterface, true )) {
107
							$goodTransformer = true;
108
							$result ["#transformers"] [$transType] [$member] = $trans;
109
						}
110
					}
111
					if (! $goodTransformer) {
112
						throw new TransformerException ( sprintf ( '%s does not implements %s', $trans, 'TransformerInterfaces' ) );
113
					}
114
				}
115
			}
116
		}
117
118 6
		foreach ( $this->joinColumnMembers as $member => $annotation ) {
119 6
			$result ["#joinColumn"] [$member] = $annotation->getPropertiesAndValues ();
120 6
			$result ["#invertedJoinColumn"] [$annotation->name] = [ "member" => $member,"className" => $annotation->className ];
121
		}
122 6
		return "return " . UArray::asPhpArray ( $result, "array" ) . ";";
123
	}
124
}
125