Issues (1783)

src/helpers/Text.php (26 issues)

1
<?php
2
/**
3
 * Retour plugin for Craft CMS
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\retour\helpers;
13
14
use Stringy\Stringy;
15
use function function_exists;
16
17
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
18
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
19
 * @package   Retour
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
20
 * @since     3.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
21
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
22
class Text
23
{
24
    // Constants
25
    // =========================================================================
26
27
    // Public Static Methods
28
    // =========================================================================
29
30
    /**
31
     * Truncates the string to a given length. If $substring is provided, and
32
     * truncating occurs, the string is further truncated so that the substring
33
     * may be appended without exceeding the desired length.
34
     *
35
     * @param string $string The string to truncate
0 ignored issues
show
Expected 4 spaces after parameter name; 1 found
Loading history...
36
     * @param int $length Desired length of the truncated string
0 ignored issues
show
Expected 4 spaces after parameter type; 1 found
Loading history...
Expected 4 spaces after parameter name; 1 found
Loading history...
37
     * @param string $substring The substring to append if it can fit
38
     *
39
     * @return string with the resulting $str after truncating
40
     */
41
    public static function truncate(string $string, int $length, string $substring = '…'): string
42
    {
43
        $result = $string;
44
45
        if (!empty($string)) {
46
            $string = strip_tags($string);
47
            $result = (string)Stringy::create($string)->truncate($length, $substring);
48
        }
49
50
        return $result;
51
    }
52
53
    /**
54
     * Truncates the string to a given length, while ensuring that it does not
55
     * split words. If $substring is provided, and truncating occurs, the
56
     * string is further truncated so that the substring may be appended without
57
     * exceeding the desired length.
58
     *
59
     * @param string $string The string to truncate
0 ignored issues
show
Expected 4 spaces after parameter name; 1 found
Loading history...
60
     * @param int $length Desired length of the truncated string
0 ignored issues
show
Expected 4 spaces after parameter type; 1 found
Loading history...
Expected 4 spaces after parameter name; 1 found
Loading history...
61
     * @param string $substring The substring to append if it can fit
62
     *
63
     * @return string with the resulting $str after truncating
64
     */
65
    public static function truncateOnWord(string $string, int $length, string $substring = '…'): string
66
    {
67
        $result = $string;
68
69
        if (!empty($string)) {
70
            $string = strip_tags($string);
71
            $result = (string)Stringy::create($string)->safeTruncate($length, $substring);
72
        }
73
74
        return $result;
75
    }
76
77
    /**
78
     * Clean up the passed in text by converting it to UTF-8, stripping tags,
79
     * removing whitespace, and decoding HTML entities
80
     *
81
     * @param string $text
0 ignored issues
show
Missing parameter comment
Loading history...
82
     *
83
     * @return string
84
     */
85
    public static function cleanupText(string $text): string
86
    {
87
        if (empty($text)) {
88
            return '';
89
        }
90
        // Convert to UTF-8
91
        if (function_exists('iconv')) {
92
            $text = iconv(mb_detect_encoding($text, mb_detect_order(), true), 'UTF-8//IGNORE', $text);
0 ignored issues
show
It seems like mb_detect_order() can also be of type true; however, parameter $encodings of mb_detect_encoding() does only seem to accept array|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            $text = iconv(mb_detect_encoding($text, /** @scrutinizer ignore-type */ mb_detect_order(), true), 'UTF-8//IGNORE', $text);
Loading history...
93
        } else {
94
            ini_set('mbstring.substitute_character', 'none');
95
            $text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');
96
        }
97
        // Strip HTML tags
98
        $text = strip_tags($text);
0 ignored issues
show
It seems like $text can also be of type array; however, parameter $string of strip_tags() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        $text = strip_tags(/** @scrutinizer ignore-type */ $text);
Loading history...
99
        // Remove excess whitespace
100
        $text = preg_replace('/\s{2,}/u', ' ', $text);
101
        // Decode any HTML entities
102
        $text = html_entity_decode($text);
103
104
        return $text;
105
    }
106
107
    // Protected Static Methods
108
    // =========================================================================
109
}
110