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

BodyParameter::getName()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SwaggerGen\Swagger;
4
5
/**
6
 * Describes a Swagger Parameter object of the "body" variety.
7
 *
8
 * @package    SwaggerGen
9
 * @author     Martijn van der Lee <[email protected]>
10
 * @copyright  2014-2016 Martijn van der Lee
11
 * @license    https://opensource.org/licenses/MIT MIT
12
 */
13
class BodyParameter extends AbstractObject implements IParameter
14
{
15
16
	private $name = '';
17
	private $description;
18
	private $required = false;
19
20
	/**
21
	 * @var Schema
22
	 */
23
	private $schema;
24
25
	public function __construct(AbstractObject $parent, $data, $required = false)
26
	{
27
		parent::__construct($parent);
28
29
		$type = self::wordShift($data);
30
		if (empty($type)) {
31
			throw new \SwaggerGen\Exception('No type definition for body parameter');
32
		}
33
34
		$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...
35
		if (empty($this->name)) {
36
			throw new \SwaggerGen\Exception('No name for body parameter');
37
		}
38
39
		$this->description = $data;
40
		$this->required = (bool) $required;
41
42
		$this->schema = new Schema($this, $type);
43
	}
44
45
	public function handleCommand($command, $data = null)
46
	{
47
		// Pass through to Type
48
		$return = $this->schema->handleCommand($command, $data);
49
		if ($return) {
50
			return $return;
51
		}
52
53
		return parent::handleCommand($command, $data);
54
	}
55
56
	public function toArray()
57
	{
58
		return self::arrayFilterNull(array_merge(array(
59
					'name' => $this->name,
60
					'in' => 'body',
61
					'description' => empty($this->description) ? null : $this->description,
62
					'required' => $this->required ? true : null,
63
					'schema' => $this->schema->toArray(),
64
								), parent::toArray()));
65
	}
66
67
	public function __toString()
68
	{
69
		return __CLASS__ . ' ' . $this->name;
70
	}
71
72
	public function getName()
73
	{
74
		return $this->name;
75
	}
76
77
}
78