PhpjobsProvider::createJobObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Job;
4
5
class PhpjobsProvider 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
            'name' => $payload['title'],
19 4
            'title' => $payload['title'],
20 4
            'url' => $payload['link'],
21 4
        ]);
22
23
        // Set date posted
24 4
        $job->setDatePostedAsString($payload['pubDate']);
25
26 4
        return $job;
27
    }
28
29
    /**
30
     * Job response object default keys that should be set
31
     *
32
     * @return  array
33
     */
34 4
    public function getDefaultResponseFields()
35
    {
36
        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...
37 4
            'description',
38 4
            'link',
39 4
            'pubDate',
40 4
            'title',
41 4
        ];
42
    }
43
44
    /**
45
     * Get format
46
     *
47
     * @return  string Currently only 'json' and 'xml' supported
48
     */
49 4
    public function getFormat()
50
    {
51 4
        return 'xml';
52
    }
53
54
    /**
55
     * Get listings path
56
     *
57
     * @return  string
58
     */
59 4
    public function getListingsPath()
60
    {
61 4
        return 'channel.item';
62
    }
63
}
64