GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ArduinoFormatter::normalizeToLimitBytes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Jamal\JenkinsArduino\Jenkins;
4
5
/**
6
 * @author Caio Almeida <[email protected]>
7
 */
8
class ArduinoFormatter
9
{
10
    const LIMIT_BYTES = 16;
11
12
    /**
13
     * @param  Build  $build
14
     * @return string
15
     */
16
    public function format(Build $build)
17
    {
18
        $firstMessage  = $this->normalizeToLimitBytes($build->getDisplayName());
19
        $secondMessage = $this->normalizeRunner($build->getActions());
20
21
        return sprintf(
22
            '%d|%s|%s',
23
            $build->getStatusCode(),
24
            $firstMessage,
25
            $secondMessage
26
        );
27
    }
28
29
    /**
30
     * @param  string $text
31
     * @return string
32
     */
33
    private function normalizeToLimitBytes($text)
34
    {
35
        if (strlen($text) > self::LIMIT_BYTES) {
36
            return substr($text, -self::LIMIT_BYTES);
37
        }
38
39
        return str_pad($text, self::LIMIT_BYTES, ' ', STR_PAD_RIGHT);
40
    }
41
42
    /**
43
     * @param  StdClass $actions
44
     * @return string
45
     */
46
    private function normalizeRunner($actions)
47
    {
48
        $formatted = '';
49
50
        foreach ($actions as $action) {
51
            if (isset($action->causes)) {
52
                $cause = $action->causes[0];
53
54
                if (isset($cause->userName)) {
55
                    $formatted = $cause->userName;
56
                } else {
57
                    $shortDescription = $cause->shortDescription;
58
                    $formatted = trim(str_replace('Started by', '', $shortDescription));
59
                }
60
            }
61
        }
62
63
        return $this->normalizeToLimitBytes($formatted);
64
    }
65
}
66