GitHub   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 0
loc 91
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A downloadPhar() 0 16 3
A getPharUrl() 0 8 2
A publishManifest() 0 14 2
A checkoutGitHubPages() 0 18 3
A getGitHubPagesDirectory() 0 4 1
1
<?php
2
3
namespace Cpliakas\ManifestPublisher\Target;
4
5
use Cpliakas\ManifestPublisher\Repository;
6
use Cpliakas\ManifestPublisher\Traits as Traits;
7
8
class GitHub implements TargetInterface
9
{
10
    use Traits\HttpClient;
11
    use Traits\Directories;
12
13
    /**
14
     * {@inheritDoc}
15
     */
16
    public function downloadPhar(Repository $repository, $version, $overwrite = false)
17
    {
18
        if (!$url = $this->getPharUrl($repository, $version)) {
19
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Cpliakas\ManifestPublish...Interface::downloadPhar 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...
20
        }
21
22
        $directory = $this->getPharDirectory($repository, $version);
23
        $filepath  = $directory . '/' . basename($url);
24
25
        try {
26
            $this->downloadFile($url, $filepath, $overwrite);
27
        } catch (\RuntimeException $e) {
28
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Cpliakas\ManifestPublish...Interface::downloadPhar 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...
29
        }
30
        return $filepath;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getPharUrl(Repository $repository, $version)
37
    {
38
        if ($filename = $repository->getPharFilename($version)) {
39
            return 'https://github.com/' . $repository . '/releases/download/' . $version . '/' . $filename;
40
        } else {
41
            return false;
42
        }
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     *
48
     * Publishes the manifest to GitHub pages.
49
     */
50
    public function publishManifest(Repository $repository, $json)
51
    {
52
        $git = $this->checkoutGitHubPages($repository);
53
54
        file_put_contents($git->getDirectory() . '/manifest.json', $json . PHP_EOL);
55
        $git->add('manifest.json');
56
57
        if ($git->hasChanges()) {
58
            $git
59
                ->commit('Manifest published by the cpliakas/manifest-publisher tool')
60
                ->push()
61
            ;
62
        }
63
    }
64
65
    /**
66
     * @param Repository $repository
67
     *
68
     * @return \GitWrapper\GitWorkingCopy
69
     */
70
    public function checkoutGitHubPages(Repository $repository)
71
    {
72
        $isProjectPage = !preg_match('#\.github\.io$#', $repository->getPackageName());
73
        $branch = $isProjectPage ? 'gh-pages' : 'master';
74
        $directory = $this->getGitHubPagesDirectory($repository);
75
        $git = $repository->getGitWrapper()->workingCopy($directory);
76
77
        if (!$git->isCloned()) {
78
            $git->clone('[email protected]:' . $repository . '.git');
79
        }
80
81
        $git
82
            ->checkout($branch)
83
            ->pull()
84
        ;
85
86
        return $git;
87
    }
88
89
    /**
90
     * @param Repository $repository
91
     *
92
     * @return string
93
     */
94
    public function getGitHubPagesDirectory(Repository $repository)
95
    {
96
        return $this->getBaseDirectory() . '/gh-pages/' . $repository;
97
    }
98
}
99