Completed
Pull Request — develop (#430)
by Narcotic
294:39 queued 229:38
created

SchemaAdditionalPropertiesHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A serializeSchemaAdditionalPropertiesToJson() 0 23 3
1
<?php
2
/**
3
 * SchemaAdditionalPropertiesHandler class file
4
 * adds the flexibility of render 'additionalProperties' in schema/swagger.json
5
 * in a proper way depending of the consumer
6
 */
7
8
namespace Graviton\SchemaBundle\Serializer\Handler;
9
10
use Graviton\SchemaBundle\Document\Schema;
11
use Graviton\SchemaBundle\Document\SchemaAdditionalProperties;
12
use JMS\Serializer\Context;
13
use JMS\Serializer\JsonSerializationVisitor;
14
15
/**
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/GPL GPL
18
 * @link     http://swisscom.ch
19
 */
20
class SchemaAdditionalPropertiesHandler
21
{
22
23
    /**
24
     * Serialize additionalProperties to JSON
25
     *
26
     * @param JsonSerializationVisitor   $visitor              Visitor
27
     * @param SchemaAdditionalProperties $additionalProperties properties
28
     * @param array                      $type                 Type
29
     * @param Context                    $context              Context
30
     *
31
     * @return string|null
32
     */
33
    public function serializeSchemaAdditionalPropertiesToJson(
34
        JsonSerializationVisitor $visitor,
35
        SchemaAdditionalProperties $additionalProperties,
36
        array $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
        Context $context
38
    ) {
39
40
        $properties = $additionalProperties->getProperties();
41
42
        // case for v4 schema
43
        if (is_bool($properties)) {
44
            return $visitor->visitBoolean($properties, [], $context);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $visitor->visitBo...es, array(), $context); (boolean) is incompatible with the return type documented by Graviton\SchemaBundle\Se...itionalPropertiesToJson of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
        }
46
47
        // case for schema inside additionalProperties, swagger exclusive
48
        if ($properties instanceof Schema) {
49
            return $visitor->getNavigator()->accept(
50
                $properties,
51
                ['name' => 'Graviton\SchemaBundle\Document\Schema'],
52
                $context
53
            );
54
        }
55
    }
56
}
57