Completed
Push — master ( e19b31...4da4bd )
by Scott
02:04
created

functions.php ➔ adjustArgument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use Exception;
5
use exussum12\CoverageChecker\Outputs\Text;
6
7
function findAutoLoader()
8
{
9
    $locations = [
10
        __DIR__ . '/../vendor/autoload.php',
11
        __DIR__ . '/../autoload.php'
12
    ];
13
14
    $found = false;
15
16
    foreach ($locations as $file) {
17
        if (file_exists($file)) {
18
            require_once($file);
19
            $found = true;
20
            break;
21
        }
22
    }
23
    // @codeCoverageIgnoreStart
24
    if (!$found) {
25
        error_log(
26
            "Can't find the autoload file," .
27
            "please make sure 'composer install' has been run"
28
        );
29
30
        exit(1);
31
    // @codeCoverageIgnoreEnd
32
    }
33
}
34
35
function checkCallIsCorrect(ArgParser $args)
36
{
37
    if (!$args->getArg(1) || !$args->getArg(2)) {
38
        throw new Exception(
39
            "Missing arguments, please call with diff and check file\n" .
40
            "e.g. vendor/bin/diffFilter --phpcs diff.txt phpcs.json",
41
            1
42
        );
43
    }
44
}
45
46
/**
47
 * @codeCoverageIgnore
48
 */
49
function adjustForStdIn($argument)
50
{
51
    if ($argument == "-") {
52
        return "php://stdin";
53
    }
54
55
    // @codeCoverageIgnoreStart
56
    if (strpos($argument, '/dev/fd') === 0) {
57
        return str_replace('/dev/fd', 'php://fd', $argument);
58
    }
59
    // @codeCoverageIgnoreEnd
60
61
    return $argument;
62
}
63
64
function getMinPercent($percent)
65
{
66
    $minimumPercentCovered = 100;
67
68
    if (is_numeric($percent)) {
69
        $minimumPercentCovered = min(
70
            $minimumPercentCovered,
71
            max(0, $percent)
72
        );
73
    }
74
75
    return $minimumPercentCovered;
76
}
77
78
function handleOutput($lines, $minimumPercentCovered, Output $output)
79
{
80
    $coveredLines = calculateLines($lines['coveredLines']);
81
    $uncoveredLines = calculateLines($lines['uncoveredLines']);
82
83
84
    if ($coveredLines + $uncoveredLines == 0) {
85
        error_log('No lines found!');
86
        return;
87
    }
88
89
    $percentCovered = 100 * ($coveredLines / ($coveredLines + $uncoveredLines));
90
91
    $output->output(
92
        $lines['uncoveredLines'],
93
        $percentCovered,
94
        $minimumPercentCovered
95
    );
96
97
    if ($percentCovered >= $minimumPercentCovered) {
98
        return;
99
    }
100
101
    throw new Exception(
102
        'Failing due to coverage being lower than threshold',
103
        2
104
    );
105
}
106
107
function calculateLines($lines)
108
{
109
    return array_sum(array_map('count', $lines));
110
}
111
112
function addExceptionHandler()
113
{
114
    set_exception_handler(
115
        function ($exception) {
116
            // @codeCoverageIgnoreStart
117
            error_log($exception->getMessage());
118
            exit($exception->getCode());
119
            // @codeCoverageIgnoreEnd
120
        }
121
    );
122
}
123
124
function getFileChecker(ArgParser $args, array $argMapper, $filename)
125
{
126
    foreach ($argMapper as $arg => $class) {
127
        if ($args->getArg($arg)) {
128
            $class = __NAMESPACE__ . '\\' . $class;
129
            return new $class($filename);
130
        }
131
    }
132
    printOptions($argMapper);
133
    throw new Exception("Can not find file handler");
134
}
135
136
function printOptions(array $arguments)
137
{
138
    $tabWidth = 8;
139
    $defaultWidth = 80;
140
141
    $width = (int) (`tput cols` ?: $defaultWidth);
142
    $width -= 2 * $tabWidth;
143
    foreach ($arguments as $argument => $class) {
144
        $class = __NAMESPACE__ . '\\' . $class;
145
146
        $argument = adjustArgument($argument, $tabWidth);
147
148
        error_log(sprintf(
149
            "%s\t%s",
150
            $argument,
151
            wordwrap(
152
                $class::getDescription(),
153
                $width,
154
                "\n\t\t",
155
                true
156
            )
157
        ));
158
    }
159
}
160
161
function adjustArgument($argument, $tabWidth)
162
{
163
    $argument = '--' . $argument;
164
    if (strlen($argument) < $tabWidth) {
165
        $argument .= "\t";
166
    }
167
    return $argument;
168
}
169