GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ApiCommand   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 199
Duplicated Lines 21.61 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
dl 43
loc 199
rs 9.2
c 0
b 0
f 0
wmc 40
lcom 3
cbo 1

16 Methods

Rating   Name   Duplication   Size   Complexity  
A buildPackages() 0 4 1
B buildKeywords() 0 21 7
A render() 0 7 1
A renderPartial() 0 5 1
A renderSourceLink() 0 7 2
A highlight() 0 6 1
A buildOfflinePages() 0 28 2
A buildModel() 0 8 1
A renderInheritance() 12 12 3
A renderImplements() 12 12 3
A renderSubclasses() 12 12 3
A renderTypeUrl() 7 7 3
A renderSubjectUrl() 0 10 3
A renderPropertySignature() 0 15 5
A fixMethodAnchor() 0 7 2
A fixOfflineLink() 0 11 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ApiCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ApiCommand, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * ApiCommand class file.
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link http://www.yiiframework.com/
7
 * @copyright 2008-2013 Yii Software LLC
8
 * @license http://www.yiiframework.com/license/
9
 */
10
11
Yii::import('frontend.commands.api.*');
12
13
/**
14
 * MessageCommand extracts messages to be translated from source files.
15
 * The extracted messages are saved as PHP message source files
16
 * under the specified directory.
17
 *
18
 * @author Qiang Xue <[email protected]>
19
 * @package system.build
20
 * @since 1.0
21
 */
22
class ApiCommand extends CConsoleCommand
23
{
24
	const URL_PATTERN='/\{\{([^\}]+)\|([^\}]+)\}\}/';
25
	public $classes;
26
	public $packages;
27
	public $pageTitle;
28
	public $themePath;
29
	public $currentClass;
30
	public $baseSourceUrl="https://github.com/shogodev/argilla/blob/master/";
31
	public $version;
32
33
	protected function buildPackages($docPath)
34
	{
35
		file_put_contents($docPath.'/packages.txt',serialize($this->packages));
36
	}
37
38
	protected function buildKeywords($docPath)
39
	{
40
		$keywords=array();
41
		foreach($this->classes as $class)
42
			$keywords[]=$class->name;
43
		foreach($this->classes as $class)
44
		{
45
			$name=$class->name;
46
			foreach($class->properties as $property)
47
			{
48
				if(!$property->isInherited)
49
					$keywords[]=$name.'.'.$property->name;
50
			}
51
			foreach($class->methods as $method)
52
			{
53
				if(!$method->isInherited)
54
					$keywords[]=$name.'.'.$method->name.'()';
55
			}
56
		}
57
		file_put_contents($docPath.'/keywords.txt',implode(',',$keywords));
58
	}
59
60
	public function render($view,$data=null,$return=false,$layout='main')
61
	{
62
		$viewFile=$this->themePath."/views/{$view}.php";
63
		$layoutFile=$this->themePath."/layouts/{$layout}.php";
64
		$content=$this->renderFile($viewFile,$data,true);
65
		return $this->renderFile($layoutFile,array('content'=>$content),$return);
66
	}
67
68
	public function renderPartial($view,$data=null,$return=false)
69
	{
70
		$viewFile=$this->themePath."/views/{$view}.php";
71
		return $this->renderFile($viewFile,$data,$return);
72
	}
73
74
	public function renderSourceLink($sourcePath,$line=null)
75
	{
76
		if($line===null)
77
			return CHtml::link('framework'.$sourcePath,$this->baseSourceUrl.$sourcePath,array('class'=>'sourceLink'));
78
		else
79
			return CHtml::link('framework'.$sourcePath.'#'.$line, $this->baseSourceUrl.$sourcePath.'#L'.$line,array('class'=>'sourceLink'));
80
	}
81
82
	public function highlight($code,$limit=20)
83
	{
84
		$code=preg_replace("/^    /m",'',rtrim(str_replace("  ","    ",$code)));
85
		$code=highlight_string("<?php\n".$code,true);
86
		return preg_replace('/&lt;\\?php<br \\/>/','',$code,1);
87
	}
88
89
	protected function buildOfflinePages($docPath,$themePath)
90
	{
91
		$this->themePath=$themePath;
92
		@mkdir($docPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
93
		$content=$this->render('index',null,true);
94
		$content=preg_replace_callback(self::URL_PATTERN,array($this,'fixOfflineLink'),$content);
95
		file_put_contents($docPath.'/index.html',$content);
96
97
		foreach($this->classes as $name=>$class)
98
		{
99
			$this->currentClass=$name;
100
			$this->pageTitle=$name;
101
			$content=$this->render('class',array('class'=>$class),true);
102
			$content=preg_replace_callback(self::URL_PATTERN,array($this,'fixOfflineLink'),$content);
103
			file_put_contents($docPath.'/'.$name.'.html',$content);
104
		}
105
106
		CFileHelper::copyDirectory($this->themePath.'/assets',$docPath);
107
108
		$content=$this->renderPartial('chmProject',null,true);
109
		file_put_contents($docPath.'/manual.hhp',$content);
110
111
		$content=$this->renderPartial('chmIndex',null,true);
112
		file_put_contents($docPath.'/manual.hhk',$content);
113
114
		$content=$this->renderPartial('chmContents',null,true);
115
		file_put_contents($docPath.'/manual.hhc',$content);
116
	}
117
118
	protected function buildModel($sourcePath,$options)
119
	{
120
		$files = CFileHelper::findFiles($sourcePath, $options);
121
122
    $model=new ApiModel;
123
		$model->build($files);
124
		return $model;
125
	}
126
127 View Code Duplication
	public function renderInheritance($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
128
	{
129
		$parents=array($class->signature);
130
		foreach($class->parentClasses as $parent)
131
		{
132
			if(isset($this->classes[$parent]))
133
				$parents[]='{{'.$parent.'|'.$parent.'}}';
134
			else
135
				$parents[]=$parent;
136
		}
137
		return implode(" &raquo;\n",$parents);
138
	}
139
140 View Code Duplication
	public function renderImplements($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
141
	{
142
		$interfaces=array();
143
		foreach($class->interfaces as $interface)
144
		{
145
			if(isset($this->classes[$interface]))
146
				$interfaces[]='{{'.$interface.'|'.$interface.'}}';
147
			else
148
				$interfaces[]=$interface;
149
		}
150
		return implode(', ',$interfaces);
151
	}
152
153 View Code Duplication
	public function renderSubclasses($class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
154
	{
155
		$subclasses=array();
156
		foreach($class->subclasses as $subclass)
157
		{
158
			if(isset($this->classes[$subclass]))
159
				$subclasses[]='{{'.$subclass.'|'.$subclass.'}}';
160
			else
161
				$subclasses[]=$subclass;
162
		}
163
		return implode(', ',$subclasses);
164
	}
165
166 View Code Duplication
	public function renderTypeUrl($type)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
167
	{
168
		if(isset($this->classes[$type]) && $type!==$this->currentClass)
169
			return '{{'.$type.'|'.$type.'}}';
170
		else
171
			return $type;
172
	}
173
174
	public function renderSubjectUrl($type,$subject,$text=null)
175
	{
176
		if($text===null)
177
			$text=$subject;
178
		if(isset($this->classes[$type])) {
179
			return '{{'.$type.'::'.$subject.'-detail'.'|'.$text.'}}';
180
		}
181
		else
182
			return $text;
183
	}
184
185
	public function renderPropertySignature($property)
186
	{
187
		if(!empty($property->signature))
188
			return $property->signature;
189
		$sig='';
190
		if(!empty($property->getter))
191
			$sig=$property->getter->signature;
192
		if(!empty($property->setter))
193
		{
194
			if($sig!=='')
195
				$sig.='<br/>';
196
			$sig.=$property->setter->signature;
197
		}
198
		return $sig;
199
	}
200
201
	public function fixMethodAnchor($class,$name)
202
	{
203
		if(isset($this->classes[$class]->properties[$name]))
204
			return $name."()";
205
		else
206
			return $name;
207
	}
208
209
	protected function fixOfflineLink($matches)
210
	{
211
		if(($pos=strpos($matches[1],'::'))!==false)
212
		{
213
			$className=substr($matches[1],0,$pos);
214
			$method=substr($matches[1],$pos+2);
215
			return "<a href=\"{$className}.html#{$method}\">{$matches[2]}</a>";
216
		}
217
		else
218
			return "<a href=\"{$matches[1]}.html\">{$matches[2]}</a>";
219
	}
220
}
221