Completed
Push — master ( 37902c...ffb897 )
by Martijn
02:31
created

AbstractObject   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 154
Duplicated Lines 5.19 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 3
cbo 0
dl 8
loc 154
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getParent() 0 4 1
A getParentClass() 0 7 2
A handleCommand() 0 9 3
A toArray() 0 4 1
A translateMimeTypes() 0 10 3
A trim() 0 4 1
A arrayFilterNull() 0 6 2
A objectsToArray() 0 6 1
A wordShift() 8 8 2
A wordSplit() 0 4 1
A getSwagger() 0 4 1

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace SwaggerGen\Swagger;
4
5
/**
6
 * Common foundation for all Swagger objects, handling the overall generator
7
 * structure, extensions and a few generic services.
8
 *
9
 * @package    SwaggerGen
10
 * @author     Martijn van der Lee <[email protected]>
11
 * @copyright  2014-2015 Martijn van der Lee
12
 * @license    https://opensource.org/licenses/MIT MIT
13
 */
14
abstract class AbstractObject
15
{
16
17
	private static $mime_types = array(
18
		'fileform' => 'multipart/form-data',
19
		'form' => 'application/x-www-form-urlencoded',
20
		'json' => 'application/json',
21
		'text' => 'text/plain',
22
		'utf8' => 'text/plain; charset=utf-8',
23
		'yml' => 'application/x-yaml',
24
		'yaml' => 'application/x-yaml',
25
		'php' => 'text/x-php',
26
		'xml' => 'text/xml',
27
	);
28
29
	/**
30
	 * @var AbstractObject
31
	 */
32
	private $parent;
33
34
	/**
35
	 * Map of extensions and their (trimmed) values
36
	 * @var string[]
37
	 */
38
	private $extensions = array();
39
40
	public function __construct(AbstractObject $parent = null)
41
	{
42
		$this->parent = $parent;
43
	}
44
45
	/**
46
	 * @return AbstractObject
47
	 */
48
	protected function getParent()
49
	{
50
		return $this->parent;
51
	}
52
53
	protected function getParentClass($classname)
54
	{
55
		if (is_a($this, $classname)) {
56
			return $this;
57
		}
58
		return $this->parent->getParentClass($classname);
59
	}
60
61
	/**
62
	 * @return \SwaggerGen\Swagger\Swagger
63
	 */
64
	protected function getSwagger()
65
	{
66
		return $this->parent->getSwagger();
67
	}
68
69
	/**
70
	 * @param string $command
71
	 * @param string $data
72
	 * @return \SwaggerGen\Swagger\AbstractObject|boolean
73
	 */
74
	public function handleCommand($command, $data = null)
75
	{
76
		if (strtolower(substr($command, 0, 2)) === 'x-') {
77
			$this->extensions[$command] = empty($data) ? $data : trim($data);
78
			return $this;
79
		}
80
81
		return false;
82
	}
83
84
	/**
85
	 * @return array
86
	 */
87
	public function toArray()
88
	{
89
		return $this->extensions;
90
	}
91
92
	/**
93
	 * Translate consumes from shortcuts
94
	 * @param String[] $mimeTypes
95
	 * @return String[]
96
	 */
97
	protected static function translateMimeTypes($mimeTypes)
98
	{
99
		foreach ($mimeTypes as &$mimeType) {
100
			if (isset(self::$mime_types[strtolower($mimeType)])) {
101
				$mimeType = self::$mime_types[strtolower($mimeType)];
102
			}
103
		}
104
105
		return $mimeTypes;
106
	}
107
108
	/**
109
	 * Trim whitespace from a multibyte string
110
	 * @param string $string
111
	 * @return string
112
	 */
113
	public static function trim($string)
114
	{
115
		return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
116
	}
117
118
	/**
119
	 * Filter all items from an array where the value is either null or an
120
	 * empty array.
121
	 * @param Array $array
122
	 * @return Array
123
	 */
124
	public static function arrayFilterNull($array)
125
	{
126
		return array_filter($array, function($value) {
127
			return $value !== null && $value !== array();
128
		});
129
	}
130
131
	/**
132
	 * Recursively call toArray() on all objects to return an array
133
	 * @param Array $array
134
	 * @return Array
135
	 */
136
	public static function objectsToArray(&$array)
137
	{
138
		return array_map(function(AbstractObject $item) {
139
			return $item->toArray();
140
		}, $array);
141
	}
142
143
	/**
144
	 * Shifts the first word off a text line and returns it
145
	 * @param string $data
146
	 * @return string|bool Either the first word or false if no more words available
147
	 */
148 View Code Duplication
	public static function wordShift(&$data)
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...
149
	{
150
		if (preg_match('~^\s*(\S+)\s*(.*)$~s', $data, $matches) === 1) {
151
			$data = $matches[2];
152
			return $matches[1];
153
		}
154
		return false;
155
	}
156
157
	/**
158
	 * Splits a text line in all it's words
159
	 * @param string $data
160
	 * @return string
161
	 */
162
	public static function wordSplit($data)
163
	{
164
		return array_values(preg_grep('~\S~', preg_split('~\s+~', $data)));
165
	}
166
167
}
168