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

tarlepp / symfony-flex-backend / #5622

22 Feb 2025 10:59PM UTC coverage: 100.0%. Remained the same
#5622

Pull #2945

php-coveralls

web-flow
Merge ca05c026b into d9edad4e5
Pull Request #2945: Chore(ci) - GitHub actions image update

2299 of 2299 relevant lines covered (100.0%)

92.02 hits per line

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

100.0
/src/Collection/CollectionTrait.php
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Collection/CollectionTrait.php
5
 *
6
 * @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
7
 */
8

9
namespace App\Collection;
10

11
use CallbackFilterIterator;
12
use Closure;
13
use InvalidArgumentException;
14
use IteratorAggregate;
15
use IteratorIterator;
16
use Throwable;
17
use function iterator_count;
18

19
/**
20
 * @package App\Collection
21
 * @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
22
 */
23
trait CollectionTrait
24
{
25
    /**
26
     * Method to filter current collection.
27
     *
28
     * @psalm-var class-string $className
29
     */
30
    abstract public function filter(string $className): Closure;
31

32
    /**
33
     * Method to process error message for current collection.
34
     *
35
     * @psalm-var class-string $className
36
     *
37
     * @throws InvalidArgumentException
38
     */
39
    abstract public function getErrorMessage(string $className): string;
40

41
    /**
42
     * Getter method for given class for current collection.
43
     *
44
     * @throws InvalidArgumentException
45
     */
46
    public function get(string $className): mixed
47
    {
48
        return $this->getFilteredItem($className)
92✔
49
            ?? throw new InvalidArgumentException($this->getErrorMessage($className));
92✔
50
    }
51

52
    /**
53
     * Method to get all items from current collection.
54
     *
55
     * @return IteratorAggregate<mixed>
56
     */
57
    public function getAll(): IteratorAggregate
58
    {
59
        return $this->items;
2✔
60
    }
61

62
    /**
63
     * Method to check if specified class exists or not in current collection.
64
     */
65
    public function has(?string $className = null): bool
66
    {
67
        return $this->getFilteredItem($className ?? '') !== null;
95✔
68
    }
69

70
    /**
71
     * Count elements of an object.
72
     */
73
    public function count(): int
74
    {
75
        return iterator_count($this->items);
1✔
76
    }
77

78
    private function getFilteredItem(string $className): mixed
79
    {
80
        try {
81
            $iterator = $this->items->getIterator();
113✔
82
        } catch (Throwable $throwable) {
1✔
83
            $this->logger->error($throwable->getMessage());
1✔
84

85
            return null;
1✔
86
        }
87

88
        $filteredIterator = new CallbackFilterIterator(new IteratorIterator($iterator), $this->filter($className));
112✔
89
        $filteredIterator->rewind();
112✔
90

91
        return $filteredIterator->current();
112✔
92
    }
93
}
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

© 2026 Coveralls, Inc