Passed
Push — master ( 31de2d...8df2bc )
by Scott
33s
created

functions.php ➔ getFileChecker()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 3
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use Exception;
5
6
function findAutoLoader()
7
{
8
    $locations = [
9
        __DIR__ . '/../vendor/autoload.php',
10
        __DIR__ . '/../autoload.php'
11
    ];
12
13
    $found = false;
14
15
    foreach ($locations as $file) {
16
        if (file_exists($file)) {
17
            require_once($file);
18
            $found = true;
19
            break;
20
        }
21
    }
22
    // @codeCoverageIgnoreStart
23
    if (!$found) {
24
        error_log(
25
            "Can't find the autoload file," .
26
            "please make sure 'composer install' has been run"
27
        );
28
29
        exit(1);
30
    // @codeCoverageIgnoreEnd
31
    }
32
}
33
34
function checkCallIsCorrect(ArgParser $args)
35
{
36
    if (!$args->getArg(1) || !$args->getArg(2)) {
37
        throw new Exception(
38
            "Missing arguments, please call with diff and check file",
39
            1
40
        );
41
    }
42
}
43
44
/**
45
 * @codeCoverageIgnore
46
 */
47
function adjustForStdIn($argument)
48
{
49
    if ($argument == "-") {
50
        return "php://stdin";
51
    }
52
53
    return $argument;
54
}
55
56
function getMinPercent($percent)
57
{
58
    $minimumPercentCovered = 100;
59
60
    if (is_numeric($percent)) {
61
        $minimumPercentCovered = min(
62
            $minimumPercentCovered,
63
            max(0, $percent)
64
        );
65
    }
66
67
    return $minimumPercentCovered;
68
}
69
70
function handleOutput($lines, $minimumPercentCovered)
71
{
72
    $coveredLines = calculateLines($lines['coveredLines']);
73
    $uncoveredLines = calculateLines($lines['uncoveredLines']);
74
75
76
    if ($coveredLines + $uncoveredLines == 0) {
77
        echo "No lines found!";
78
        return;
79
    }
80
    $percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines));
81
    
82
    $extra = PHP_EOL;
83
84
    if ($lines['uncoveredLines']) {
85
        $extra = ', Missed lines '.
86
            $extra .
87
            print_r($lines['uncoveredLines'], true)
88
        ;
89
    }
90
91
    printf('%.2f%% Covered%s', $percentCovered, $extra);
92
    
93
    if ($percentCovered >= $minimumPercentCovered) {
94
        return;
95
    }
96
97
    throw new Exception(
98
        "Failing due to coverage being lower than threshold",
99
        2
100
    );
101
}
102
103
function calculateLines($lines)
104
{
105
    return count($lines, COUNT_RECURSIVE) - count($lines);
106
}
107
108
function addExceptionHandler()
109
{
110
    set_exception_handler(
111
        function (Exception $exception) {
112
            // @codeCoverageIgnoreStart
113
            error_log(get_class($exception));
114
            error_log($exception->getMessage());
115
            exit($exception->getCode());
116
            // @codeCoverageIgnoreEnd
117
        }
118
    );
119
}
120
121
function getFileChecker(ArgParser $args, array $argMappper, $filename)
122
{
123
    foreach ($argMappper as $arg => $class) {
124
        if ($args->getArg($arg)) {
125
            $class = __NAMESPACE__ . '\\' . $class;
126
            return new $class($filename);
127
        }
128
    }
129
130
    throw new Exception("Can not find file handler");
131
}
132