Semaphore   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A release() 0 4 2
B fetch() 0 11 5
1
<?php
0 ignored issues
show
Coding Style introduced by
The opening PHP tag must be the first content in the file
Loading history...
2
/* zLibrary
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Affero General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Affero General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
class Semaphore
19
{
20
    /**
21
     * @param int $semaphoreID
22
     * @return null|resource
23
     */
24
    public static function fetch($semaphoreID)
25
    {
26
        global $useSemaphores, $semaphoreModulus;
27
        if (!isset($useSemaphores)) $useSemaphores = false;
28
        if ($useSemaphores == false) return null;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
29
        if (!isset($semaphoreModulus)) $semaphoreModulus = 10;
30
31
        $sem = sem_get($semaphoreID % $semaphoreModulus);
32
        if (!sem_acquire($sem)) return null;
33
        return $sem;
34
    }
35
36
    /**
37
     * @param null|resource $semaphoreResource
38
     */
39
    public static function release($semaphoreResource)
40
    {
41
        if ($semaphoreResource != null) sem_release($semaphoreResource);
42
    }
43
}
44