System::inContainer()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 4
b 0
f 1
nc 3
nop 0
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace AlecRabbit\Helpers\Classes;
5
6
/**
7
 * Class System
8
 *
9
 * @internal
10
 */
11
final class System
12
{
13
    protected const CGROUP_FILE = '/proc/1/cgroup';
14
    protected const CGROUP_PATTERN = '(lxc|docker|kubepods)';
15
16
    /**
17
     * Determine if run in container
18
     *
19
     * @return bool
20
     *
21
     * @codeCoverageIgnore
22
     *
23
     * @link https://stackoverflow.com/a/20012536
24
     */
25
    public static function inContainer(): bool
26
    {
27
        return
28
            file_exists(self::CGROUP_FILE)
29
            && ($content = file_get_contents(self::CGROUP_FILE))
30
            && preg_match(self::CGROUP_PATTERN, $content);
31
    }
32
}
33