Passed
Pull Request — master (#16)
by Eric
12:51
created

FormatterTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 11
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A formatDataForDisplay() 0 21 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\SimpleCounter.
7
 *
8
 * (c) Eric Sizemore <https://github.com/ericsizemore>
9
 *
10
 * This source file is subject to the MIT license. For the full copyright and
11
 * license information, please view the LICENSE file that was distributed with
12
 * this source code.
13
 */
14
15
namespace Esi\SimpleCounter\Trait;
16
17
use Esi\SimpleCounter\Interface\ConfigurationInterface;
18
19
use function array_map;
20
use function implode;
21
use function number_format;
22
use function sprintf;
23
use function str_split;
24
25
use const DIRECTORY_SEPARATOR;
26
27
/**
28
 * Trait used in *Storage classes.
29
 */
30
trait FormatterTrait
31
{
32
    /**
33
     * Returns the formatted count information given the current count.
34
     *
35
     * If the 'asImage' option is set to true, then HTML is returned. Otherwise,
36
     * just plain text.
37
     *
38
     * Normally called in the Storage class' display() method.
39
     */
40 7
    protected function formatDataForDisplay(ConfigurationInterface $configuration, int $currentCount): string
41
    {
42
        /** @var string $imageDir */
43 7
        $imageDir = $configuration::getOption('imageDir');
44
45
        /** @var string $imageExt */
46 7
        $imageExt = $configuration::getOption('imageExt');
47
48
        /** @var string $visitorTextString */
49 7
        $visitorTextString = $configuration::getOption('visitorTextString');
50
51 7
        if ($configuration::getOption('asImage') === true) {
52 1
            return implode('&nbsp;', array_map(static fn (string $number): string => sprintf(
53 1
                '<img src="%s%d%s" alt="%2$d" />',
54 1
                $imageDir . DIRECTORY_SEPARATOR,
55 1
                $number,
56 1
                $imageExt,
57 1
            ), str_split((string) $currentCount)));
1 ignored issue
show
Bug introduced by
It seems like str_split((string)$currentCount) can also be of type true; however, parameter $array of array_map() does only seem to accept array, 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

57
            ), /** @scrutinizer ignore-type */ str_split((string) $currentCount)));
Loading history...
58
        }
59
60 6
        return sprintf($visitorTextString, number_format((float) $currentCount));
61
    }
62
}
63