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

contributte / datagrid / 8230471191

11 Mar 2024 09:25AM UTC coverage: 34.102%. First build
8230471191

Pull #1060

github

radimvaculik
Fix phpstan
Pull Request #1060: [7.x] Next

117 of 435 new or added lines in 54 files covered. (26.9%)

1124 of 3296 relevant lines covered (34.1%)

0.34 hits per line

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

0.0
/src/AggregationFunction/FunctionSum.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Datagrid\AggregationFunction;
4

5
use Contributte\Datagrid\Utils\PropertyAccessHelper;
6
use Dibi\Fluent;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\QueryBuilder;
9
use Nette\Database\Table\Selection;
10
use Nextras\Orm\Collection\DbalCollection;
11
use Nextras\Orm\Collection\ICollection;
12

13
class FunctionSum implements ISingleColumnAggregationFunction
14
{
15

16
        protected int $result = 0;
17

18
        /** @var callable|null */
19
        protected $renderer = null;
20

NEW
21
        public function __construct(protected string $column, protected string $dataType = IAggregationFunction::DATA_TYPE_PAGINATED)
×
22
        {
23
        }
24

25
        public function getFilterDataType(): string
26
        {
27
                return $this->dataType;
×
28
        }
29

30
        public function processDataSource(Fluent|QueryBuilder|Collection|Selection|ICollection $dataSource): void
31
        {
32
                if ($dataSource instanceof Fluent) {
×
33
                        $connection = $dataSource->getConnection();
×
34
                        $this->result = (int) $connection->select('SUM(%n)', $this->column)
×
35
                                ->from($dataSource, 's')
×
36
                                ->fetchSingle();
×
37
                }
38

39
                if ($dataSource instanceof QueryBuilder) {
×
NEW
40
                        $column = str_contains($this->column, '.')
×
41
                                ? $this->column
×
42
                                : current($dataSource->getRootAliases()) . '.' . $this->column;
×
43

NEW
44
                        $this->result = (int) $dataSource
×
45
                                ->select(sprintf('SUM(%s)', $column))
×
46
                                ->setMaxResults(1)
×
47
                                ->setFirstResult(0)
×
48
                                ->getQuery()
×
49
                                ->getSingleScalarResult();
×
50
                }
51

52
                if ($dataSource instanceof Collection) {
×
53
                        $dataSource->forAll(function ($key, $value): bool {
×
54
                                $this->result += PropertyAccessHelper::getValue($value, $this->column);
55

56
                                return true;
57
                        });
×
58
                }
59

NEW
60
                if ($dataSource instanceof DbalCollection) {
×
NEW
61
                        foreach ($dataSource->fetchAll() as $item)
×
NEW
62
                                $this->result += $item->getValue($this->column);
×
63
                }
64
        }
65

66
        public function renderResult(): mixed
67
        {
68
                $result = $this->result;
×
69

70
                if (isset($this->renderer)) {
×
71
                        $result = call_user_func($this->renderer, $result);
×
72
                }
73

74
                return $result;
×
75
        }
76

77
        /**
78
         * @return static
79
         */
80
        public function setRenderer(?callable $callback = null): self
81
        {
82
                $this->renderer = $callback;
×
83

84
                return $this;
×
85
        }
86

87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc