LatestRelease   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A latest() 0 19 4
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Services\Update;
13
14
use Jitamin\Foundation\Base;
15
16
/**
17
 * A class to get the latest release tag for Github.
18
 */
19
class LatestRelease extends Base
20
{
21
    const CACHE_TIME_IN_HOURS = 1;
22
23
    /**
24
     * @var string
25
     */
26
    private $github_url = 'https://api.github.com/repos/jitamin/jitamin/releases/latest';
27
28
    /**
29
     * Get the latest release from Github.
30
     *
31
     * @return string
32
     */
33
    public function latest()
34
    {
35
        $cache_for = self::CACHE_TIME_IN_HOURS * 60;
36
37
        if ($release = $this->container['cacheDriver']->get('jitamin_latest_version')) {
38
            return $release;
39
        } else {
40
            $body = $this->container['httpClient']->getJson($this->github_url, ['Accept: application/vnd.github.v3+json']);
41
42
            if (is_array($body) && isset($body['tag_name'])) {
43
                $release = $body['tag_name'];
44
                $this->container['cacheDriver']->set('jitamin_latest_version', $release, $cache_for);
45
46
                return $release;
47
            }
48
        }
49
50
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Jitamin\Services\Update\LatestRelease::latest 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...
51
    }
52
}
53