Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

LatestPostField   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A resolve() 0 7 1
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/20/16 12:07 AM
7
*/
8
9
namespace Examples\Blog\Schema;
10
11
12
use BlogTest\PostType;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Examples\Blog\Schema\PostType.

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...
13
use Youshido\GraphQL\Execution\ResolveInfo;
14
use Youshido\GraphQL\Field\AbstractField;
15
16
class LatestPostField extends AbstractField
17
{
18
    public function getType()
19
    {
20
        return new PostType();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \BlogTest\PostType(); (BlogTest\PostType) is incompatible with the return type declared by the interface Youshido\GraphQL\Field\FieldInterface::getType of type Youshido\GraphQL\Type\AbstractType.

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...
21
    }
22
23
    public function resolve($value, array $args, ResolveInfo $info)
24
    {
25
        return [
26
            "title"   => "New approach in API has been revealed",
27
            "summary" => "This post describes a new approach to create and maintain APIs",
28
        ];
29
    }
30
31
32
}
33