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

luttje / laravel-user-custom-id / 24599874799

18 Apr 2026 07:31AM UTC coverage: 93.719% (+0.06%) from 93.655%
24599874799

push

github

luttje
Fix formatting

13 of 13 new or added lines in 2 files covered. (100.0%)

3 existing lines in 1 file now uncovered.

373 of 398 relevant lines covered (93.72%)

235.15 hits per line

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

88.89
/src/FormatChunkRepository.php
1
<?php
2

3
namespace Luttje\UserCustomId;
4

5
use Illuminate\Support\Facades\File;
6
use Luttje\UserCustomId\FormatChunks\FormatChunk;
7
use Luttje\UserCustomId\FormatChunks\FormatChunkCollection;
8

9
class FormatChunkRepository
10
{
11
    protected $registeredChunkTypes = [];
12

13
    /**
14
     * Goes through all the files in the FormatChunks directory
15
     * and registers all the chunk types.
16
     */
17
    public function registerDefaultChunkTypes()
636✔
18
    {
19
        $files = File::allFiles(__DIR__ . '/FormatChunks');
636✔
20

21
        foreach ($files as $file) {
636✔
22
            $class = 'Luttje\\UserCustomId\\' . str_replace(
636✔
23
                ['/', '.php'],
636✔
24
                ['\\', ''],
636✔
25
                trim(substr($file, strlen(__DIR__)), '/\\')
636✔
26
            );
636✔
27

28
            if (! is_subclass_of($class, FormatChunk::class)) {
636✔
29
                continue;
636✔
30
            }
31

32
            if ((new \ReflectionClass($class))->isAbstract()) {
636✔
33
                continue;
636✔
34
            }
35

36
            $this->registerChunkType($class);
636✔
37
        }
38
    }
39

40
    /**
41
     * Registers a new type of chunk (e.g: {increment})
42
     */
43
    public function registerChunkType(string $chunkType)
636✔
44
    {
45
        if (! is_subclass_of($chunkType, FormatChunk::class)) {
636✔
46
            throw new \Exception('The given chunk type must be an instance of FormatChunk.');
×
47
        }
48

49
        $id = $chunkType::getChunkId();
636✔
50

51
        $this->registeredChunkTypes[$id] = $chunkType;
636✔
52
    }
53

54
    /**
55
     * Returns the chunk type for the given chunk id.
56
     */
57
    public function getChunkType(string $id): ?string
624✔
58
    {
59
        return $this->registeredChunkTypes[$id] ?? null;
624✔
60
    }
61

62
    /**
63
     * Returns all registered chunk types.
64
     */
UNCOV
65
    public function getChunkTypes(): array
×
66
    {
UNCOV
67
        return $this->registeredChunkTypes;
×
68
    }
69

70
    /**
71
     * Given a part of the format string, this method will find
72
     * the appropriate chunk type and return an instance of it.
73
     *
74
     * An example of a format string is: {increment:5:00000}
75
     *
76
     * The lastValueChunks parameter contains the last value that was
77
     * generated for this format. It's already parsed into chunks (because
78
     * that is how it's saved in the database)
79
     *
80
     * @param  string  $chunkString  The chunk string to parse (e.g: {increment:5:00000})
81
     * @param  FormatChunkCollection  $lastValueChunks  The last value that was generated for this format.
82
     */
83
    public function getChunk(string $chunkString, ?FormatChunkCollection $lastValueChunks = null): ?FormatChunk
120✔
84
    {
85
        $chunkString = trim($chunkString, '{}');
120✔
86
        $parts = explode(':', $chunkString);
120✔
87
        $id = $parts[0];
120✔
88
        $chunkType = $this->getChunkType($id);
120✔
89

90
        if (! $chunkType) {
120✔
UNCOV
91
            return null;
×
92
        }
93

94
        /** @var FormatChunk $chunk */
95
        $chunk = new $chunkType(...array_slice($parts, 1));
120✔
96

97
        if ($lastValueChunks) {
120✔
98
            foreach ($lastValueChunks as $lastValueChunk) {
48✔
99
                if ($lastValueChunk->getChunkId() === $id) {
48✔
100
                    $chunk->setValue($lastValueChunk->getValue());
48✔
101
                    break;
48✔
102
                }
103
            }
104
        }
105

106
        return $chunk;
120✔
107
    }
108
}
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