• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 30001290505

23 Jul 2026 10:57AM UTC coverage: 70.309% (-1.7%) from 72.022%
30001290505

push

github

NamelessCoder
[TER] 8.0.0

4819 of 6854 relevant lines covered (70.31%)

6.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.18
/Classes/ViewHelpers/Math/AverageViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Math;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Traits\ArgumentOverride;
12
use FluidTYPO3\Vhs\Traits\ArrayConsumingViewHelperTrait;
13

14
/**
15
 * Math: Average
16
 *
17
 * Performs average across an array. If $a is an array and
18
 * $b is an array, each member of $a is averaged against the
19
 * same member in $b. If $a is an array and $b is a number,
20
 * each member of $a is averaged agained $b. If $a is an array
21
 * this array is averaged to one number. If $a is a number and
22
 * $b is not provided or NULL, $a is gracefully returned as an
23
 * average value of itself.
24
 */
25
class AverageViewHelper extends AbstractMultipleMathViewHelper
26
{
27
    use ArrayConsumingViewHelperTrait;
28
    use ArgumentOverride;
29

30
    public function initializeArguments(): void
31
    {
32
        parent::initializeArguments();
3✔
33
        $this->overrideArgument('b', 'mixed', 'Optional: Second number or Iterator/Traversable/Array for calculation');
3✔
34
    }
35

36
    /**
37
     * @param mixed $a
38
     * @param mixed $b
39
     * @return mixed
40
     */
41
    protected static function calculateAction($a, $b, array $arguments)
42
    {
43
        $aIsIterable = static::assertIsArrayOrIterator($a);
3✔
44
        $bIsIterable = static::assertIsArrayOrIterator($b);
3✔
45
        if ($aIsIterable) {
3✔
46
            $a = static::arrayFromArrayOrTraversableOrCSVStatic($a);
×
47
            if ($b === null) {
×
48
                return array_sum($a) / count($a);
×
49
            }
50
            if ($bIsIterable) {
×
51
                $b = static::arrayFromArrayOrTraversableOrCSVStatic($b);
×
52
            }
53
            foreach ($a as $index => $value) {
×
54
                $bSide = is_array($b) ? $b[$index] : $b;
×
55
                $a[$index] = static::calculateAction($value, $bSide, $arguments);
×
56
            }
57
            return $a;
×
58
        }
59
        if (null === $b) {
3✔
60
            return $a;
×
61
        }
62
        return ($a + $b) / 2;
3✔
63
    }
64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc