Github::getFileContents()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 3
nop 2
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 View Code Duplication
class Github implements RepositoryManager
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @var \Github\Client
20
     */
21
    private $client;
22
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->client = new \Github\Client();
29
    }
30
31
    /**
32
     * Get the file contents.
33
     *
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('repo')->contents()->download(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Github\Api\ApiInterface as the method contents() does only exist in the following implementations of said interface: Github\Api\Repo.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

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