AbstractVcsDriver::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
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 Webcreate\Conveyor\Repository\Driver;
13
14
use Webcreate\Conveyor\IO\IOInterface;
15
use Webcreate\Conveyor\Repository\Version;
16
use Webcreate\Vcs\Common\Reference;
17
use Webcreate\Vcs\VcsInterface;
18
19
abstract class AbstractVcsDriver implements DriverInterface
20
{
21
    protected $url;
22
23
    /**
24
     * @var VcsInterface
25
     */
26
    protected $client;
27
    protected $io;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
28
    protected $cacheDir;
29
30
    public function __construct($url, IOInterface $io = null)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
31
    {
32
        $this->url = $url;
33
        $this->io = $io;
34
    }
35
36 1
    public function initialize($url)
37
    {
38 1
        $this->client = $this->getClient($url);
39 1
    }
40
41
    /**
42
     * @param $url
43
     * @return VcsInterface
44
     */
45
    abstract protected function getClient($url);
46
47 1
    public function setCacheDir($cacheDir)
48
    {
49 1
        $this->cacheDir = $cacheDir;
50 1
    }
51
52 1
    public function getVersions()
53
    {
54 1
        $versions = array();
55
56 1
        $tags = $this->client->tags();
57 1
        foreach ($tags as $tag) {
58 1
            $versions[] = $this->createVersion($tag->getName(), $tag->getRevision());
59
        }
60
61 1
        $branches = $this->client->branches();
62 1
        foreach ($branches as $branch) {
63 1
            $versions[] = $this->createVersion($branch->getName(), $branch->getRevision(), 'dev');
64
        }
65
66 1
        return $versions;
67
    }
68
69
    public function getHead(Version $version)
70
    {
71
        if ($version->getName() == 'dev-trunk') {
72
            $head = new Reference('trunk');
73
        } elseif (substr($version->getName(), 0, 4) == 'dev-') {
74
            $head = new Reference(substr($version->getName(), 4));
75
        } else {
76
            $head = new Reference($version->getName(), Reference::TAG);
77
        }
78
79
        return $head;
80
    }
81
82
    /**
83
     * @param string $name
84
     * @param string $revision
85
     * @param string $prefix
86
     * @return \Webcreate\Conveyor\Repository\Version
87
     */
88 1
    protected function createVersion($name, $revision, $prefix = null)
89
    {
90 1
        $name = ($prefix ? $prefix . '-' : '') . $name;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $name. This often makes code more readable.
Loading history...
91
92 1
        $version = new Version();
93 1
        $version->setName($name);
94 1
        $version->setBuild($revision);
95
96 1
        return $version;
97
    }
98
99
    public function export(Version $version, $dest)
100
    {
101
        $head = $this->getHead($version);
102
        $this->client->setHead($head);
103
104
        /*
105
         * @todo Collect output of export process
106
         *
107
         * if ($this->io) {
108
         *   $io = $this->io;
109
         *   $this->client->setOutput(function ($type, $output) use ($io) {
110
         *       if ($io->isVerbose()) {
111
         *           $io->write($output, false);
112
         *       }
113
         *   });
114
         *}
115
         */
116
117
        $result = $this->client->export('', $dest);
118
119
        //$this->client->setOutput(null);
120
        return $result;
121
    }
122
123
    public function diff(Version $oldVersion, Version $newVersion)
124
    {
125
        $head = $this->getHead($oldVersion);
126
        $this->client->setHead($head);
127
128
        $result = $this->client->diff(
129
            $this->getHead($oldVersion), $this->getHead($newVersion),
0 ignored issues
show
Documentation introduced by
$this->getHead($oldVersion) is of type object<Webcreate\Vcs\Common\Reference>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->getHead($newVersion) is of type object<Webcreate\Vcs\Common\Reference>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
            $oldVersion->getBuild(), $newVersion->getBuild()
131
        );
132
133
        return $result;
134
    }
135
136
    public function revisionCompare($revision1, $revision2)
137
    {
138
        return $this->client->revisionCompare($revision1, $revision2);
139
    }
140
141
    public function changelog(Version $version1, Version $version2)
142
    {
143
        return $this->client->changelog($version1->getBuild(), $version2->getBuild());
144
    }
145
}
146