Str   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A lineSeparator() 0 3 1
A formatAsTable() 0 13 3
B stringify() 0 19 7
A startsWith() 0 3 2
A contains() 0 3 2
A prefix() 0 7 2
A endsWith() 0 3 1
1
<?php
2
3
namespace Bencagri\Artisan\Deployer\Helper;
4
5
6
class Str
7
{
8
    public static function startsWith(string $haystack, string $needle) : bool
9
    {
10
        return '' !== $needle && 0 === mb_strpos($haystack, $needle);
11
    }
12
13
    public static function endsWith(string $haystack, string $needle) : bool
14
    {
15
        return $needle === mb_substr($haystack, -mb_strlen($needle));
16
    }
17
18
    public static function contains(string $haystack, string $needle) : bool
19
    {
20
        return '' !== $needle && false !== mb_strpos($haystack, $needle);
21
    }
22
23
    public static function lineSeparator(string $char = '-') : string
24
    {
25
        return str_repeat($char, 80);
26
    }
27
28
    public static function prefix($text, string $prefix) : string
29
    {
30
        $text = is_array($text) ? $text : explode(PHP_EOL, $text);
31
32
        return implode(PHP_EOL, array_map(function ($line) use ($prefix) {
33
            return $prefix.$line;
34
        }, $text));
35
    }
36
37
    public static function stringify($value) : string
38
    {
39
        if (is_resource($value)) {
40
            return 'PHP Resource';
41
        }
42
43
        if (is_bool($value)) {
44
            return $value ? 'true' : 'false';
45
        }
46
47
        if (is_array($value)) {
48
            return json_encode($value, JSON_UNESCAPED_SLASHES);
49
        }
50
51
        if (is_object($value) && !method_exists($value, '__toString')) {
52
            return json_encode($value, JSON_UNESCAPED_SLASHES);
53
        }
54
55
        return (string) $value;
56
    }
57
58
    public static function formatAsTable(array $data, bool $sortKeys = true) : string
59
    {
60
        if ($sortKeys) {
61
            ksort($data);
62
        }
63
64
        $arrayAsString = '';
65
        $longestArrayKeyLength = max(array_map('strlen', array_keys($data)));
66
        foreach ($data as $key => $value) {
67
            $arrayAsString .= sprintf("%s%s : %s\n", $key, str_repeat(' ', $longestArrayKeyLength - mb_strlen($key)), self::stringify($value));
68
        }
69
70
        return rtrim($arrayAsString);
71
    }
72
}
73