TwigReadingTimeExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 22
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 4 1
A getName() 0 3 1
A readingtimeFilter() 0 8 2
1
<?php
2
3
namespace Flynt\Utils;
4
5
use Twig_Extension;
6
use Twig\TwigFilter;
7
8
class TwigReadingTimeExtension extends Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead ( Ignorable by Annotation )

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

8
class TwigReadingTimeExtension extends /** @scrutinizer ignore-deprecated */ Twig_Extension
Loading history...
9
{
10
    public function getName()
11
    {
12
        return 'ReadingTime';
13
    }
14
15
    public function getFilters()
16
    {
17
        return [
18
            new TwigFilter('readingtime', [$this, 'readingtimeFilter'])
19
        ];
20
    }
21
22
    public function readingtimeFilter($content)
23
    {
24
        $wordsPerMinute = 200;
25
        $words = str_word_count(strip_tags($content));
26
        $minutesToRead = floor($words / $wordsPerMinute);
27
        $min = ($minutesToRead < 1 ? '1' : $minutesToRead);
28
29
        return $min;
30
    }
31
}
32