PHPUnitBinFile::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.5021

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 6
cts 11
cp 0.5455
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 5.5021
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paraunit\Configuration;
6
7
/**
8
 * Class PHPUnitBinFile
9
 * @package Paraunit\Configuration
10
 */
11
class PHPUnitBinFile
12
{
13
    // I'm using Paraunit as a vendor package
14
    private const PHPUNIT_REALPATH_FOR_VENDOR = '/../../../../../phpunit/phpunit/phpunit';
15
    // I'm using Paraunit standalone (developing)
16
    private const PHPUNIT_REALPATH_FOR_STANDALONE = '/../../../vendor/phpunit/phpunit/phpunit';
17
18
    /** @var string Realpath to PHPUnit bin location */
19
    private $phpUnitBin;
20
21
    /**
22
     * PHPUnitBinFile constructor.
23
     * @throws \RuntimeException If PHPUnit is not found
24
     */
25 41
    public function __construct()
26
    {
27 41
        if (\defined('PARAUNIT_PHAR_FILE')) {
28
            // Paraunit is running as a standalone PHAR archive
29
            // PHPUnit is embedded in the archive, self execute it in special mode
30
            $this->phpUnitBin = PARAUNIT_PHAR_FILE . ' phpunit';
31
32
            return;
33
        }
34
35 41
        if (file_exists(__DIR__ . self::PHPUNIT_REALPATH_FOR_VENDOR)) {
36
            $this->setPhpUnitBin(__DIR__ . self::PHPUNIT_REALPATH_FOR_VENDOR);
37
38
            return;
39
        }
40
41 41
        if (file_exists(__DIR__ . self::PHPUNIT_REALPATH_FOR_STANDALONE)) {
42 41
            $this->setPhpUnitBin(__DIR__ . self::PHPUNIT_REALPATH_FOR_STANDALONE);
43
44 41
            return;
45
        }
46
47
        throw new \RuntimeException('PHPUnit bin not found');
48
    }
49
50 41
    public function getPhpUnitBin(): string
51
    {
52 41
        return $this->phpUnitBin;
53
    }
54
55
    private function setPhpUnitBin($phpUnitBin): void
56
    {
57
        $realpath = realpath($phpUnitBin);
58
        if (! $realpath) {
59
            throw new \RuntimeException('Unable set PHPUnit binary real path');
60
        }
61
62
        $this->phpUnitBin = $realpath;
63
    }
64
}
65