Completed
Pull Request — master (#9)
by Viacheslav
39:48
created

Operation::setUpProperties()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 80
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 8.8387
c 0
b 0
f 0
cc 1
eloc 75
nc 1
nop 2

How to fix   Long Method   

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
/**
3
 * @file ATTENTION!!! The code below was carefully crafted by a mean machine.
4
 * Please consider to NOT put any emotional human-generated modifications as AI will throw them away with no mercy.
5
 */
6
7
namespace Swaggest\JsonSchema\SwaggerSchema;
8
9
use Swaggest\JsonSchema\Constraint\Properties;
10
use Swaggest\JsonSchema\Schema;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Swaggest\JsonSchema\SwaggerSchema\Schema.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Swaggest\JsonSchema\Structure\ClassStructure;
12
13
14
class Operation extends ClassStructure {
15
	/** @var string[]|array */
16
	public $tags;
17
18
	/** @var string A brief summary of the operation. */
19
	public $summary;
20
21
	/** @var string A longer description of the operation, GitHub Flavored Markdown is allowed. */
22
	public $description;
23
24
	/** @var ExternalDocs information about external documentation */
25
	public $externalDocs;
26
27
	/** @var string A unique identifier of the operation. */
28
	public $operationId;
29
30
	/** @var string[]|array A list of MIME types the API can produce. */
31
	public $produces;
32
33
	/** @var string[]|array A list of MIME types the API can consume. */
34
	public $consumes;
35
36
	/** @var BodyParameter[]|HeaderParameterSubSchema[]|FormDataParameterSubSchema[]|QueryParameterSubSchema[]|PathParameterSubSchema[]|JsonReference[]|array The parameters needed to send a valid API call. */
37
	public $parameters;
38
39
	/** @var Response[]|JsonReference[] Response objects names can either be any valid HTTP status code or 'default'. */
40
	public $responses;
41
42
	/** @var string[]|array The transfer protocol of the API. */
43
	public $schemes;
44
45
	/** @var bool */
46
	public $deprecated;
47
48
	/** @var string[][]|array[][]|array */
49
	public $security;
50
51
	/**
52
	 * @param Properties|static $properties
53
	 * @param Schema $ownerSchema
54
	 */
55
	public static function setUpProperties($properties, Schema $ownerSchema)
56
	{
57
		$properties->tags = Schema::arr();
58
		$properties->tags->items = Schema::string();
59
		$properties->tags->uniqueItems = true;
60
		$properties->summary = Schema::string();
61
		$properties->summary->description = 'A brief summary of the operation.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
		$properties->description = Schema::string();
63
		$properties->description->description = 'A longer description of the operation, GitHub Flavored Markdown is allowed.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
64
		$properties->externalDocs = ExternalDocs::schema();
65
		$properties->operationId = Schema::string();
66
		$properties->operationId->description = 'A unique identifier of the operation.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
		$properties->produces = new Schema();
68
		$properties->produces->allOf[0] = Schema::arr();
69
		$properties->produces->allOf[0]->items = Schema::string();
70
		$properties->produces->allOf[0]->items->description = 'The MIME type of the HTTP message.';
71
		$properties->produces->allOf[0]->uniqueItems = true;
72
		$properties->produces->description = 'A list of MIME types the API can produce.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
73
		$properties->consumes = new Schema();
74
		$properties->consumes->allOf[0] = Schema::arr();
75
		$properties->consumes->allOf[0]->items = Schema::string();
76
		$properties->consumes->allOf[0]->items->description = 'The MIME type of the HTTP message.';
77
		$properties->consumes->allOf[0]->uniqueItems = true;
78
		$properties->consumes->description = 'A list of MIME types the API can consume.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
79
		$properties->parameters = Schema::arr();
80
		$properties->parameters->items = new Schema();
81
		$properties->parameters->items->oneOf[0] = new Schema();
82
		$properties->parameters->items->oneOf[0]->oneOf[0] = BodyParameter::schema();
83
		$properties->parameters->items->oneOf[0]->oneOf[1] = Schema::object();
84
		$properties->parameters->items->oneOf[0]->oneOf[1]->oneOf[0] = HeaderParameterSubSchema::schema();
85
		$properties->parameters->items->oneOf[0]->oneOf[1]->oneOf[1] = FormDataParameterSubSchema::schema();
86
		$properties->parameters->items->oneOf[0]->oneOf[1]->oneOf[2] = QueryParameterSubSchema::schema();
87
		$properties->parameters->items->oneOf[0]->oneOf[1]->oneOf[3] = PathParameterSubSchema::schema();
88
		$properties->parameters->items->oneOf[0]->oneOf[1]->required = array (
89
		  0 => 'name',
90
		  1 => 'in',
91
		  2 => 'type',
92
		);
93
		$properties->parameters->items->oneOf[1] = JsonReference::schema();
94
		$properties->parameters->description = 'The parameters needed to send a valid API call.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
		$properties->parameters->uniqueItems = true;
96
		$properties->responses = Schema::object();
97
		$properties->responses->additionalProperties = false;
98
		$properties->responses->patternProperties['^([0-9]{3})$|^(default)$'] = new Schema();
99
		$properties->responses->patternProperties['^([0-9]{3})$|^(default)$']->oneOf[0] = Response::schema();
100
		$properties->responses->patternProperties['^([0-9]{3})$|^(default)$']->oneOf[1] = JsonReference::schema();
101
		$properties->responses->patternProperties['^x-'] = new Schema();
102
		$properties->responses->patternProperties['^x-']->description = 'Any property starting with x- is valid.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
		$properties->responses->not = Schema::object();
104
		$properties->responses->not->additionalProperties = false;
105
		$properties->responses->not->patternProperties['^x-'] = new Schema();
106
		$properties->responses->not->patternProperties['^x-']->description = 'Any property starting with x- is valid.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
		$properties->responses->description = 'Response objects names can either be any valid HTTP status code or \'default\'.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
		$properties->responses->minProperties = 1;
109
		$properties->schemes = Schema::arr();
110
		$properties->schemes->items = Schema::string();
111
		$properties->schemes->items->enum = array (
112
		  0 => 'http',
113
		  1 => 'https',
114
		  2 => 'ws',
115
		  3 => 'wss',
116
		);
117
		$properties->schemes->description = 'The transfer protocol of the API.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
		$properties->schemes->uniqueItems = true;
119
		$properties->deprecated = Schema::boolean();
120
		$properties->deprecated->default = false;
0 ignored issues
show
Documentation introduced by
The property default does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121
		$properties->security = Schema::arr();
122
		$properties->security->items = Schema::object();
123
		$properties->security->items->additionalProperties = Schema::arr();
124
		$properties->security->items->additionalProperties->items = Schema::string();
125
		$properties->security->items->additionalProperties->uniqueItems = true;
126
		$properties->security->uniqueItems = true;
127
		$ownerSchema->type = 'object';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'object' of type string is incompatible with the declared type object<Swaggest\JsonSchema\Constraint\Type> of property $type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
128
		$ownerSchema->additionalProperties = false;
129
		$ownerSchema->patternProperties['^x-'] = new Schema();
130
		$ownerSchema->patternProperties['^x-']->description = 'Any property starting with x- is valid.';
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<Swaggest\JsonSchema\Schema>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
131
		$ownerSchema->required = array (
132
		  0 => 'responses',
133
		);
134
	}
135
}
136
137