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

OndraM / simple-google-reader / 22279327646

22 Feb 2026 02:49PM UTC coverage: 91.935% (-0.5%) from 92.424%
22279327646

push

github

OndraM
Chore: Upgrade PHP syntax

1 of 1 new or added line in 1 file covered. (100.0%)

3 existing lines in 2 files now uncovered.

57 of 62 relevant lines covered (91.94%)

8.31 hits per line

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

96.88
/src/Spreadsheets/SpreadsheetsReader.php
1
<?php
2

3
namespace OndraM\SimpleGoogleReader\Spreadsheets;
4

5
use Cocur\Slugify\Slugify;
6
use Google\Client;
7
use Google\Service\Sheets;
8
use Psr\SimpleCache\CacheInterface;
9

10
readonly class SpreadsheetsReader
11
{
12
    private const DEFAULT_TTL = 3600;
13

14
    public function __construct(protected Client $googleClient, private Slugify $slugify, private CacheInterface $cache) {}
15

16
    /**
17
     * @return array<int, array<string, mixed>>
18
     */
19
    public function readById(string $spreadsheetId, ?string $sheetName = null, int $ttl = self::DEFAULT_TTL): array
20
    {
21
        $cacheKey = $this->generateCacheKey($spreadsheetId, $sheetName);
15✔
22
        if ($this->cache->has($cacheKey)) {
15✔
23
            /** @var array<int, array<string, mixed>> */
UNCOV
24
            return (array) $this->cache->get($cacheKey, []);
×
25
        }
26

27
        $sheetService = $this->createService();
15✔
28
        $range = 'A:Z';
15✔
29
        if ($sheetName !== null) {
15✔
30
            $range = $sheetName . '!' . $range;
10✔
31
        }
32

33
        /** @var Sheets\Resource\SpreadsheetsValues $spreadsheetValues */
34
        $spreadsheetValues = $sheetService->spreadsheets_values;
15✔
35
        $response = $spreadsheetValues->get($spreadsheetId, $range);
15✔
36
        /** @var array<int, array<int, string>> $rows */
37
        $rows = $response->getValues();
15✔
38
        // @phpstan-ignore function.alreadyNarrowedType
39
        if (!is_array($rows) || count($rows) === 0) {
15✔
40
            return [];
5✔
41
        }
42

43
        $header = array_map(
10✔
44
            fn($value) => $this->slugify->slugify($value, ['separator' => '_']),
10✔
45
            array_shift($rows),
10✔
46
        );
10✔
47

48
        // Map rows to associative arrays based on header
49
        $data = array_map(
10✔
50
            fn($value) => array_combine($header, array_pad($value, count($header), null)),
10✔
51
            $rows,
10✔
52
        );
10✔
53

54
        // Filter out rows where all values are null
55
        $data = array_filter(
10✔
56
            $data,
10✔
57
            fn($row) => count(array_filter($row, fn($value) => $value !== null)) > 0,
10✔
58
        );
10✔
59

60
        $this->cache->set($cacheKey, $data, $ttl);
10✔
61

62
        return $data;
10✔
63
    }
64

65
    private function generateCacheKey(string $spreadsheetId, ?string $sheetName = null): string
66
    {
67
        return 'spreadsheet_'
15✔
68
            . sha1(
15✔
69
                $spreadsheetId
15✔
70
                . ($sheetName !== null ? '_' . $sheetName : ''),
15✔
71
            );
15✔
72
    }
73

74
    private function createService(): Sheets
75
    {
76
        return new Sheets($this->googleClient);
15✔
77
    }
78
}
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