StringHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 89
c 0
b 0
f 0
wmc 11
lcom 0
cbo 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A title() 0 4 1
A tidy() 0 30 5
A date() 0 8 2
A firstNWords() 0 30 3
1
<?php
2
3
namespace ReliQArts\Mardin\Helpers;
4
5
use tidy;
6
use Carbon\Carbon;
7
8
class StringHelper
9
{
10
    /**
11
     * Capatilize first letter of each word of a string.
12
     *
13
     * @param string  $value
14
     * @return string
15
     */
16
    public static function title($value)
17
    {
18
        return mb_convert_case($value, MB_CASE_TITLE);
19
    }
20
21
    /**
22
     * @param $value
23
     * @internal param $html
24
     * @return string
25
     */
26
    public static function tidy($value)
27
    {
28
        // Check to see if Tidy is available.
29
        if (class_exists('tidy')) {
30
            $tidy = new tidy();
31
32
            return  $tidy->repairString($value, [
33
                'show-body-only' => true,
34
            ]);
35
        } else { // No Tidy, Time for regex and possibly a broken DOM :(
36
            preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $value, $result);
37
            $openedtags = $result[1];
38
            preg_match_all('#</([a-z]+)>#iU', $value, $result);
39
            $closedtags = $result[1];
40
            $len_opened = count($openedtags);
41
            if (count($closedtags) == $len_opened) {
42
                return $value;
43
            }
44
            $openedtags = array_reverse($openedtags);
45
            for ($i = 0; $i < $len_opened; $i++) {
46
                if (! in_array($openedtags[$i], $closedtags)) {
47
                    $value .= '</'.$openedtags[$i].'>';
48
                } else {
49
                    unset($closedtags[array_search($openedtags[$i], $closedtags)]);
50
                }
51
            }
52
53
            return $value;
54
        }
55
    }
56
57
    public static function date(Carbon $date)
58
    {
59
        if ($date->diffInDays(Carbon::now()) < 7) {
60
            return $date->diffForHumans();
61
        } else {
62
            return $date->toFormattedDateString();
63
        }
64
    }
65
66
    public static function firstNWords($content = null, $wordsreturned = 20, $suffix = ' ...')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
    {
68
        //return "";
69
        $string = '';
70
        if ($content) {
71
            $string = $content;
72
        }
73
        /*  Returns the first $wordsreturned out of $string.  If string
74
        contains fewer words than $wordsreturned, the entire string
75
        is returned.
76
        */
77
78
        $retval = $string;      //  Just in case of a problem
0 ignored issues
show
Unused Code introduced by
$retval is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79
80
        $array = explode(' ', $string);
81
82
        if (count($array) <= $wordsreturned) {
83
            /*  Already short enough, return the whole thing
84
        */
85
            $retval = $string;
86
        } else {
87
            /*  Need to chop of some words
88
        */
89
            array_splice($array, $wordsreturned);
90
            $retval = implode(' ', $array);
91
            $retval .= $suffix;
92
        }
93
94
        return $retval;
95
    }
96
}
97