Completed
Push — master ( 61603b...2e6646 )
by Alec
02:56
created

System::inContainer()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.0488

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 3
nop 0
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 5.0488
rs 9.6111
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Helpers\Classes;
4
5
/**
6
 * Class System
7
 *
8
 * @internal
9
 */
10
final class System
11
{
12
    protected const CGROUP_FILE = '/proc/1/cgroup';
13
    protected const CGROUP_PATTERN = '(lxc|docker|kubepods)';
14
15
    /**
16
     * Determine if run in container
17
     *
18
     * @return bool
19
     *
20
     * @link https://stackoverflow.com/a/20012536
21
     */
22 2
    public static function inContainer(): bool
23
    {
24 2
        if (file_exists(self::CGROUP_FILE)
25 2
            && ($content = file_get_contents(self::CGROUP_FILE))
26 2
            && preg_match(self::CGROUP_PATTERN, $content)) {
27
            return true;
28
        }
29 2
        if(isset($content)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
30 2
            var_dump('Contents: ' . $content);
0 ignored issues
show
Security Debugging Code introduced by
var_dump('Contents: ' . $content) looks like debug code. Are you sure you do not want to remove it?
Loading history...
31
        }
32 2
        return false;
33
    }
34
}
35