Completed
Push — 3.x ( 2fafa3...bde009 )
by Vincent
06:01
created

StringExtension::deprecatedTruncate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Twig\Extension;
15
16
use Symfony\Component\String\UnicodeString as SymfonyUnicodeString;
17
use Twig\Environment;
18
use Twig\Extension\AbstractExtension;
19
use Twig\Extensions\TextExtension;
20
use Twig\TwigFilter;
21
22
/**
23
 * NEXT_MAJOR: Remove this class.
24
 *
25
 * Decorates `Twig\Extra\String\StringExtension` in order to provide the `$cut`
26
 * argument for `Symfony\Component\String\UnicodeString::truncate()`.
27
 * This class must be removed when the component ships this feature.
28
 *
29
 * @internal
30
 *
31
 * @see https://github.com/symfony/symfony/pull/35649
32
 *
33
 * @author Javier Spagnoletti <[email protected]>
34
 */
35
final class StringExtension extends AbstractExtension
36
{
37
    /**
38
     * @var TextExtension
39
     */
40
    private $legacyExtension;
41
42
    public function __construct(?TextExtension $legacyExtension = null)
43
    {
44
        $this->legacyExtension = $legacyExtension;
45
    }
46
47
    /**
48
     * @return TwigFilter[]
49
     */
50
    public function getFilters(): array
51
    {
52
        return [
53
            new TwigFilter('sonata_truncate', [$this, 'deprecatedTruncate'], ['needs_environment' => true]),
54
        ];
55
    }
56
57
    /**
58
     * @return SymfonyUnicodeString|string
59
     */
60
    public function deprecatedTruncate(Environment $env, ?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...')
61
    {
62
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
            'The "sonata_truncate" twig filter is deprecated'
64
            .' since sonata-project/admin-bundle 3.x and will be removed in 4.0. Use "u.truncate" instead.',
65
            E_USER_DEPRECATED
66
        );
67
68
        if (null !== $this->legacyExtension) {
69
            return twig_truncate_filter($env, $text, $length, $preserve, $ellipsis);
0 ignored issues
show
Compatibility introduced by
$env of type object<Twig\Environment> is not a sub-type of object<Twig_Environment>. It seems like you assume a child class of the class Twig\Environment to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
        }
71
72
        return $this->legacyTruncteWithUnicodeString($text, $length, $preserve, $ellipsis);
73
    }
74
75
    /**
76
     * NEXT_MAJOR: Fix the arguments in order to respect the signature at `UnicodeString::truncate()`.
77
     */
78
    public function legacyTruncteWithUnicodeString(?string $text, int $length = 30, bool $preserve = false, string $ellipsis = '...'): SymfonyUnicodeString
79
    {
80
        return (new SymfonyUnicodeString($text ?? ''))->truncate($length, $ellipsis, $preserve);
81
    }
82
}
83