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.
Completed
Push — master ( c752e6...86709a )
by cao
17:38
created

FileLock   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B lock() 0 17 5
A unlock() 0 9 2
1
<?php
2
namespace PhpBoot\Lock;
3
4
use PhpBoot\Utils\SafeFileWriter;
5
6
class FileLock implements LockInterface
7
{
8
    private $file;
9
    public function lock($key, $ttl)
10
    {
11
        if($this->locked){
12
            \PhpBoot\abort("relock $key");
13
        }
14
        $path = sys_get_temp_dir().'/lock_252a8fdc9b944af99a9bc53d2aea08f1_'.$key;
15
        $this->file = @fopen($path, 'a');
16
        if (!$this->file || !flock($this->file, LOCK_EX | LOCK_NB)) {
17
            if($this->file){
18
                fclose($this->file);
19
            }
20
            return false;
21
        } else {
22
            $this->locked = true;
23
        }
24
        return true;
25
    }
26
27
    public function unlock($key)
28
    {
29
        $this->locked or \PhpBoot\abort("unlock unlocked $key");
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
30
        flock($this->file, LOCK_UN);
31
        fclose($this->file);
32
        $this->file = null;
33
        $this->locked = false;
34
        return true;
35
    }
36
    private $locked = false;
37
}