Passed
Push — master ( 819559...3ee0c8 )
by Caen
03:54
created

getCoverage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
define('TIME_START', microtime(true));
4
5
/**
6
 * @internal This script is used to ping the CI server with the type coverage results.
7
 *
8
 * @example php __FILE__ ${{ secrets.CI_SERVER_TOKEN }} ${{ github.event.pull_request.head.sha }} ${{ github.head_ref }}
9
 *
10
 * @uses vendor/bin/psalm > psalmout.txt
11
 */
12
echo "Pinging CI server\n";
13
14
$token = $argv[1] ?? exit(400);
15
$commit = $argv[2] ?? exit(400);
16
$branch = $argv[3] ?? 'master';
17
18
// Very inefficient, but it works fine for our purposes.
19
function getCoverage(string $contents): float
20
{
21
    $lines = explode(PHP_EOL, $contents);
22
    foreach ($lines as $line) {
23
        if (str_starts_with($line, 'Psalm was able to infer types for ') && str_ends_with($line, '% of the codebase')) {
24
            return (float) substr($line, 34, -16);
25
        }
26
    }
27
    throw new \Exception('Could not find coverage in Psalm output');
28
}
29
30
$data = [
31
    'commit' => $commit,
32
    'coverage' => getCoverage(file_get_contents('psalmout.txt')),
33
    'time_ms' => (microtime(true) - TIME_START) * 1000,
34
];
35
36
$url = 'https://ci.hydephp.se/api/github/actions/type-coverage';
37
38
$curl = curl_init($url);
39
curl_setopt($curl, CURLOPT_URL, $url);
40
curl_setopt($curl, CURLOPT_POST, true);
41
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
42
43
$headers = [
44
    'Accept: application/json',
45
    "Authorization: Bearer $token",
46
    'Content-Type: application/json',
47
];
48
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
49
50
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
51
52
$resp = curl_exec($curl);
53
curl_close($curl);
54
var_dump($resp);
55