StackoverflowProvider::createJobObject()   B
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
ccs 17
cts 17
cp 1
rs 8.6845
cc 4
eloc 14
nc 2
nop 1
crap 4
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Job;
4
5
class StackoverflowProvider extends AbstractProvider
6
{
7
    /**
8
     * Returns the standardized job object
9
     *
10
     * @param array $payload
11
     *
12
     * @return \JobApis\Jobs\Client\Job
13
     */
14 4
    public function createJobObject($payload)
15
    {
16 4
        $job = new Job([
17 4
            'description' => $payload['description'],
18 4
            'location' => $payload['location'],
19 4
            'name' => $payload['title'],
20 4
            'title' => $payload['title'],
21 4
            'url' => $payload['link'],
22 4
        ]);
23
24
        // Set date posted
25 4
        $job->setDatePostedAsString($payload['pubDate']);
26
27
        // Set skills
28 4
        if (isset($payload['category']) && is_array($payload['category'])) {
29 2
            $skills = [];
30 2
            foreach ($payload['category'] as $category) {
31 2
                $skills[] = $category;
32 2
            }
33 2
            $job->setSkills(implode(', ', $skills));
34 2
        }
35
36 4
        return $job;
37
    }
38
39
    /**
40
     * Job response object default keys that should be set
41
     *
42
     * @return  array
43
     */
44 4
    public function getDefaultResponseFields()
45
    {
46
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('descriptio...', 'pubDate', 'title'); (string[]) is incompatible with the return type declared by the abstract method JobApis\Jobs\Client\Prov...etDefaultResponseFields of type string.

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...
47 4
            'description',
48 4
            'link',
49 4
            'location',
50 4
            'pubDate',
51 4
            'title',
52 4
        ];
53
    }
54
55
    /**
56
     * Get format
57
     *
58
     * @return  string Currently only 'json' and 'xml' supported
59
     */
60 4
    public function getFormat()
61
    {
62 4
        return 'xml';
63
    }
64
65
    /**
66
     * Get listings path
67
     *
68
     * @return  string
69
     */
70 4
    public function getListingsPath()
71
    {
72 4
        return 'channel.item';
73
    }
74
}
75