GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Locker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 20.99 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 14
c 4
b 0
f 1
lcom 1
cbo 4
dl 17
loc 81
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A openLockFile() 0 13 3
A writeToLockFile() 0 9 1
A getServer() 0 8 2
A getServers() 0 4 1
A addServer() 0 4 1
A emptyLocker() 0 4 1
A toArray() 8 8 2
A toJson() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace SeleniumSetup\Locker;
3
4
use SeleniumSetup\FileSystem;
5
use SeleniumSetup\SeleniumSetup;
6
7
class Locker
8
{
9
    protected $fileSystem;
10
    /**
11
     * @var ServerItem[]
12
     */
13
    protected $locker = [];
14
15
    public function __construct()
16
    {
17
        $this->fileSystem = new FileSystem();
18
    }
19
20
    public function openLockFile()
21
    {
22
        if (!$this->fileSystem->isFile(SeleniumSetup::$APP_ROOT_PATH . DIRECTORY_SEPARATOR . SeleniumSetup::DEFAULT_LOCK_FILENAME)) {
23
            return false;
24
        }
25
26
        $contents = $this->fileSystem->readFile(SeleniumSetup::$APP_ROOT_PATH . DIRECTORY_SEPARATOR . SeleniumSetup::DEFAULT_LOCK_FILENAME);
27
        $lockerRaw = json_decode($contents);
28
        foreach ($lockerRaw as $serverObj) {
29
            $this->addServer(ServerItemFactory::createFromObj($serverObj));
30
        }
31
        return true;
32
    }
33
34
    public function writeToLockFile()
35
    {   
36
        $this->fileSystem->writeToFile(
37
            SeleniumSetup::$APP_ROOT_PATH . DIRECTORY_SEPARATOR . SeleniumSetup::DEFAULT_LOCK_FILENAME,
38
            $this->toJson()
39
        );
40
        
41
        return true;
42
    }
43
44
    public function getServer($name)
45
    {
46
        if (!isset($this->locker[$name])) {
47
            throw new \Exception('Unknown server.');
48
        }
49
50
        return $this->locker[$name];
51
    }
52
53
    public function getServers()
54
    {
55
        return $this->locker;
56
    }
57
58
    public function addServer(ServerItem $server)
59
    {
60
        $this->locker[$server->getName()] = $server;
61
    }
62
    
63
    public function emptyLocker()
64
    {
65
        $this->locker = [];
66
    }
67
    
68 View Code Duplication
    public function toArray()
0 ignored issues
show
Duplication introduced by
This method 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...
69
    {
70
        $result = [];
71
        foreach ($this->locker as $server) {
72
            $result[$server->getName()] = $server->toArray();
73
        }
74
        return $result;
75
    }
76
    
77 View Code Duplication
    public function toJson()
0 ignored issues
show
Duplication introduced by
This method 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...
78
    {
79
        $result = [];
80
        foreach ($this->locker as $server) {
81
            $result[$server->getName()] = $server->toArray();
82
        }
83
        
84
        return json_encode($result, JSON_PRETTY_PRINT);
85
    }
86
87
}