Passed
Push — v1 ( 4d0d15...04fe15 )
by Andrew
05:36 queued 02:45
created

Text   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 22
dl 0
loc 83
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanupText() 0 20 3
A truncate() 0 10 2
A truncateOnWord() 0 10 2
1
<?php
2
/**
3
 * Webperf plugin for Craft CMS 3.x
4
 *
5
 * Monitor the performance of your webpages through real-world user timing data
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\webperf\helpers;
12
13
use Stringy\Stringy;
0 ignored issues
show
Bug introduced by
The type Stringy\Stringy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
16
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
17
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
18
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
19
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
class Text
21
{
22
    // Constants
23
    // =========================================================================
24
25
    // Public Static Methods
26
    // =========================================================================
27
28
    /**
29
     * Truncates the string to a given length. If $substring is provided, and
30
     * truncating occurs, the string is further truncated so that the substring
31
     * may be appended without exceeding the desired length.
32
     *
33
     * @param  string $string    The string to truncate
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
34
     * @param  int    $length    Desired length of the truncated string
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
35
     * @param  string $substring The substring to append if it can fit
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
36
     *
37
     * @return string with the resulting $str after truncating
38
     */
39
    public static function truncate($string, $length, $substring = '…'): string
40
    {
41
        $result = $string;
42
43
        if (!empty($string)) {
44
            $string = strip_tags($string);
45
            $result = (string)Stringy::create($string)->truncate($length, $substring);
46
        }
47
48
        return $result;
49
    }
50
51
    /**
52
     * Truncates the string to a given length, while ensuring that it does not
53
     * split words. If $substring is provided, and truncating occurs, the
54
     * string is further truncated so that the substring may be appended without
55
     * exceeding the desired length.
56
     *
57
     * @param  string $string    The string to truncate
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
58
     * @param  int    $length    Desired length of the truncated string
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
59
     * @param  string $substring The substring to append if it can fit
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
60
     *
61
     * @return string with the resulting $str after truncating
62
     */
63
    public static function truncateOnWord($string, $length, $substring = '…'): string
64
    {
65
        $result = $string;
66
67
        if (!empty($string)) {
68
            $string = strip_tags($string);
69
            $result = (string)Stringy::create($string)->safeTruncate($length, $substring);
70
        }
71
72
        return $result;
73
    }
74
75
    /**
76
     * Clean up the passed in text by converting it to UTF-8, stripping tags,
77
     * removing whitespace, and decoding HTML entities
78
     *
79
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
80
     *
81
     * @return string
82
     */
83
    public static function cleanupText($text): string
84
    {
85
        if (empty($text)) {
86
            return '';
87
        }
88
        // Convert to UTF-8
89
        if (\function_exists('iconv')) {
90
            $text = iconv(mb_detect_encoding($text, mb_detect_order(), true), 'UTF-8//IGNORE', $text);
91
        } else {
92
            ini_set('mbstring.substitute_character', 'none');
93
            $text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');
94
        }
95
        // Strip HTML tags
96
        $text = strip_tags($text);
97
        // Remove excess whitespace
98
        $text = preg_replace('/\s{2,}/u', ' ', $text);
99
        // Decode any HTML entities
100
        $text = html_entity_decode($text);
101
102
        return $text;
103
    }
104
105
    // Protected Static Methods
106
    // =========================================================================
107
}
108