Completed
Push — master ( cfeaa0...522b4d )
by Martijn
03:14
created

Parameter::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwaggerGen\Swagger;
4
5
/**
6
 * Describes a Swagger Parameter object of all varieties except "body".
7
 *
8
 * @package    SwaggerGen
9
 * @author     Martijn van der Lee <[email protected]>
10
 * @copyright  2014-2015 Martijn van der Lee
11
 * @license    https://opensource.org/licenses/MIT MIT
12
 */
13
class Parameter extends AbstractObject implements IParameter
14
{
15
16
	private static $classTypes = array(
17
		'integer' => 'Integer',
18
		'int' => 'Integer',
19
		'int32' => 'Integer',
20
		'int64' => 'Integer',
21
		'long' => 'Integer',
22
		'float' => 'Number',
23
		'double' => 'Number',
24
		'string' => 'String',
25
		'byte' => 'String',
26
		'binary' => 'String',
27
		'password' => 'String',
28
		'enum' => 'String',
29
		'boolean' => 'Boolean',
30
		'bool' => 'Boolean',
31
		'array' => 'Array',
32
		'csv' => 'Array',
33
		'ssv' => 'Array',
34
		'tsv' => 'Array',
35
		'pipes' => 'Array',
36
		'multi' => 'Array',
37
		'date' => 'Date',
38
		'datetime' => 'Date',
39
		'date-time' => 'Date',
40
		'file' => 'File',
41
			//'set'		=> 'EnumArray',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
	);
43
	private $name = '';
44
	private $in;
45
	private $description;
46
	private $required = false;
47
48
	/**
49
	 * @var Type\AbstractType
50
	 */
51
	private $Type;
52
53
	/**
54
	 * Returns true if the "multi" array collectionFormat is allowed for this
55
	 * parameter.
56
	 * @return boolean
57
	 */
58
	public function isMulti()
59
	{
60
		return in_array($this->in, array('query', 'form'));
61
	}
62
63
	/**
64
	 * Return true if the parameter is of type 'formData'
65
	 * @return type
66
	 */
67
	public function isForm()
68
	{
69
		return $this->in === 'form';
70
	}
71
72
	public function __construct(AbstractObject $parent, $in, $data, $required = false)
73
	{
74
		parent::__construct($parent);
75
76
		if ($in !== 'path' && $in !== 'form' && $in !== 'query' && $in !== 'header') {
77
			throw new \SwaggerGen\Exception("Invalid in for parameter: '{$in}'");
78
		}
79
		$this->in = $in;
80
81
		$definition = self::wordShift($data);
82
		if (empty($definition)) {
83
			throw new \SwaggerGen\Exception('No type definition for parameter');
84
		}
85
86
		$this->name = self::wordShift($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like self::wordShift($data) can also be of type false. However, the property $name is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
87
		if (empty($this->name)) {
88
			throw new \SwaggerGen\Exception('No name for parameter');
89
		}
90
91
		$this->description = $data;
92
		$this->required = (bool) $required;
93
94
		// Parse regex
95
		$match = array();
96
		$count = preg_match('/^([a-z]+)/i', $definition, $match);
0 ignored issues
show
Unused Code introduced by
$count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
		$format = strtolower($match[1]);
98 View Code Duplication
		if (isset(self::$classTypes[$format])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99
			$type = self::$classTypes[$format];
100
			$class = "SwaggerGen\\Swagger\\Type\\{$type}Type";
101
			$this->Type = new $class($this, $definition);
102
		} else {
103
			throw new \SwaggerGen\Exception("Type format not recognized: '{$format}'");
104
		}
105
	}
106
107 View Code Duplication
	public function handleCommand($command, $data = null)
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...
108
	{
109
		// Pass through to Type
110
		if ($this->Type && $this->Type->handleCommand($command, $data)) {
111
			return $this;
112
		}
113
114
		return parent::handleCommand($command, $data);
115
	}
116
117
	public function toArray()
118
	{
119
		return self::arrayFilterNull(array_merge(array(
120
					'name' => $this->name,
121
					'in' => $this->in === 'form' ? 'formData' : $this->in,
122
					'description' => empty($this->description) ? null : $this->description,
123
					'required' => $this->in === 'path' || $this->required ? true : null,
124
								), $this->Type->toArray(), parent::toArray()));
125
	}
126
127
	public function __toString()
128
	{
129
		return __CLASS__ . " {$this->name} {$this->in}";
130
	}
131
132
	public function getName()
133
	{
134
		return $this->name;
135
	}
136
137
}
138