Completed
Push — master ( ecaef0...28b78e )
by Peter
10:06
created

Gitlab::parseJson()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of Packy.
5
 *
6
 * (c) Peter Nijssen
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 AppBundle\RepositoryManager;
13
14
use AppBundle\Entity\Project;
15
16
class Gitlab implements RepositoryManager
17
{
18
    /**
19
     * @var \Gitlab\Client
20
     */
21
    private $client;
22
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->client = new \Gitlab\Client('');
29
        $this->client->authenticate('', \Gitlab\Client::AUTH_URL_TOKEN);
30
    }
31
32
    /**
33
     * Get the file contents.
34
     *
35
     * @param Project $project
36
     * @param string  $file
37
     *
38
     * @return array
39
     */
40
    public function getFileContents(Project $project, string $file): array
41
    {
42
        try {
43
            $fileContent = $this->client->api('repositories')->getFile(
0 ignored issues
show
Bug introduced by
The method getFile does only exist in Gitlab\Api\Repositories, but not in Gitlab\Api\DeployKeys an...gs and Gitlab\Api\Users.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
44
                $project->getVendorName() . '/' . $project->getPackageName(),
45
                $file,
46
                $project->getBranch()
47
            );
48
49
            return $this->parseJson($fileContent);
50
        } catch (\Gitlab\Exception\RuntimeException $e) {
51
            return [];
52
        }
53
    }
54
55
    /**
56
     * Parse JSON data.
57
     *
58
     * @param string $data
59
     *
60
     * @return mixed
61
     */
62
    private function parseJson($data)
63
    {
64
        if ($data === false) {
65
            throw new \RuntimeException('Unable to parse json file');
66
        }
67
68
        if (array_key_exists('content', $data)) {
69
            return json_decode(base64_decode($data['content']), true);
70
        }
71
72
        return '';
73
    }
74
}
75