Completed
Push — master ( 66c020...88a4bc )
by Enrico
01:49
created

Thing::asTurtleFragment()   C

Complexity

Conditions 10
Paths 3

Size

Total Lines 54
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 9
cts 9
cp 1
rs 6.8372
c 0
b 0
f 0
cc 10
eloc 34
nc 3
nop 0
crap 10

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
namespace BOTK\Model;
3
4
class Thing extends AbstractModel implements \BOTK\ModelInterface  
5
{
6
	
7
	protected static $DEFAULT_OPTIONS  = array(
8
		'uri'				=> array(
9
								'filter'    => FILTER_CALLBACK,
10
		                        'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
11
                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
12
			                   ),
13
		'base'				=> array(
14
								'default'	=> 'urn:local:',
15
								'filter'    => FILTER_CALLBACK,
16
		                        'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
17
                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
18
			                   ),
19
		'id'				=> array(
20
								'filter'    => FILTER_CALLBACK,
21
		                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_ID',
22
                            	'flags'  	=> FILTER_REQUIRE_SCALAR,
23
			                   ),
24
		'page'				=> array(	
25
								'filter'    => FILTER_CALLBACK,
26
		                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL',
27
                            	'flags'  	=> FILTER_FORCE_ARRAY,
28
			                   ),
29
		'homepage'			=> array(	
30
								'filter'    => FILTER_CALLBACK,
31
		                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL',
32
                            	'flags'  	=> FILTER_FORCE_ARRAY,
33
			                   ),
34
		'disambiguatingDescription'=> array(	
35
								'filter'    => FILTER_DEFAULT,
36
                            	'flags'  	=> FILTER_FORCE_ARRAY,
37
			                   ),
38
		'subject'			=> array(	
39
								'filter'    => FILTER_CALLBACK,
40
		                        'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
41
                            	'flags'  	=> FILTER_FORCE_ARRAY,
42
			                   ),
43
		'image'			=> array(	
44
								'filter'    => FILTER_CALLBACK,
45
		                        'options' 	=> '\BOTK\Filters::FILTER_SANITIZE_HTTP_URL',
46
                            	'flags'  	=> FILTER_FORCE_ARRAY,
47
			                   ),
48
		'sameas'			=> array(	
49
								'filter'    => FILTER_CALLBACK,
50
		                        'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
51
                            	'flags'  	=> FILTER_FORCE_ARRAY,
52
			                   ),
53
		'name'				=> array(		
54
								'filter'    => FILTER_DEFAULT,
55
                            	'flags'  	=> FILTER_FORCE_ARRAY,
56
			                   ),
57
		'alternateName'		=> array(		
58
								'filter'    => FILTER_DEFAULT,
59
                            	'flags'  	=> FILTER_FORCE_ARRAY,
60
			                   ),
61
		'description'		=> array(		
62
								'filter'    => FILTER_DEFAULT,
63
                            	'flags'  	=> FILTER_FORCE_ARRAY,
64
			                   ),
65
		'similarName'		=> array(	
66
			'filter'    => FILTER_CALLBACK,
67
			'options' 	=> '\BOTK\Filters::FILTER_VALIDATE_URI',
68
			'flags'  	=> FILTER_FORCE_ARRAY
69
			),	
70
	);
71
	
72
73
	/**
74
	 * a generic implementation that use uri, base and id property (all optionals)
75
	 */
76 14
	public function getUri()
77
	{
78 14
		if(!empty($this->data['uri'])){
79 6
			$uri =  $this->data['uri'];
80 8
		} elseif(!empty($this->data['base'])) {
81 8
			$idGenerator=$this->uniqueIdGenerator;
82 8
			$uri = $this->data['base'];
83 8
			$uri.=empty($this->data['id'])?$idGenerator($this->data):$this->data['id'];
84
		} else{
85
			$idGenerator=$this->uniqueIdGenerator;
86
			$uri = 'urn:local:botk:'.$idGenerator($this->data);
87
		}
88
		
89 14
		return $uri;
90
	}
91
92
	
93 10
	public function asTurtleFragment()
94
	{
95 10
		if(is_null($this->rdf)) {
96 10
			$uri = $this->getUri();
97
			
98
			// define $_ as a macro to write simple rdf
99 10
			$_= function($format, $var,$sanitize=true) use(&$turtleString, &$tripleCounter){
100 8
				foreach((array)$var as $v){
101 8
					if($var){
102 8
						$turtleString.= sprintf($format,$sanitize?\BOTK\Filters::FILTER_SANITIZE_TURTLE_STRING($v):$v);
103 8
						$tripleCounter++;
104
					}
105
				}
106 10
			};
107
108
	 		// serialize uri properies
109
			$tripleCounter =0;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $tripleCounter, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
110
			$turtleString="<$uri> ";
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $turtleString, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
111
			foreach (array(
112
				'page' 			=> 'foaf:page',
113
				'homepage'		=> 'foaf:homepage',
114
				'subject'		=> 'skos:subject',
115
				'image'			=> 'schema:image',
116
				'sameAs'		=> 'owl:sameAs',	
117
			) as $uriVar=>$property) {
118
				if(!empty($this->data[$uriVar])){
119
					$_("$property <%s>;", $this->data[$uriVar],false);	
120
				}
121
			}
122
			
123
			// serialize string properies
124
			foreach(array(
125
				'id'						=> 'dct:identifier',
126
				'disambiguatingDescription'	=> 'schema:disambiguatingDescription',
127
				'name'						=> 'schema:name',
128
				'alternateName'				=> 'schema:alternateName',
129
				'description'				=> 'schema:description',
130
			) as $stringVar=>$property) {
131
				if(!empty($this->data[$stringVar])){
132
					$_("$property \"%s\";", $this->data[$stringVar]);	
133
				}
134
			}
135
			
136
			if($tripleCounter){
137
				$this->rdf = substr($turtleString, 0, -1).'.';
138
				$this->tripleCount = $tripleCounter;
139
			} else {
140
				$this->rdf = ''; // no serialize if uri has no attributes
141
			}
142
			
143
		}
144
145
		return $this->rdf;
146
	}
147
}