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

68publishers / tracy-git-version / 3672506103

pending completion
3672506103

Pull #3

github

GitHub
Merge 8b6464ed0 into 50bf69527
Pull Request #3: Ci and static analysis

97 of 97 new or added lines in 21 files covered. (100.0%)

454 of 492 relevant lines covered (92.28%)

0.92 hits per line

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

76.92
/src/Repository/ExportedGitRepository.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\TracyGitVersion\Repository;
6

7
use JsonException;
8
use SixtyEightPublishers\TracyGitVersion\Repository\Command\GetHeadCommand;
9
use SixtyEightPublishers\TracyGitVersion\Repository\Command\GetLatestTagCommand;
10
use SixtyEightPublishers\TracyGitVersion\Repository\Export\CommandHandler\GetHeadCommandHandler;
11
use SixtyEightPublishers\TracyGitVersion\Repository\Export\CommandHandler\GetLatestTagCommandHandler;
12
use SixtyEightPublishers\TracyGitVersion\Repository\Export\CommandHandler\ExportedGitCommandHandlerInterface;
13
use function is_readable;
14
use function json_decode;
15
use function file_get_contents;
16

17
final class ExportedGitRepository extends AbstractGitRepository
18
{
19
        private string $file;
20

21
        private string $source;
22

23
        /** @var array<mixed, mixed>|null */
24
        private ?array $json = null;
25

26
        private ?bool $valid = null;
27

28
        /**
29
         * @param array<class-string, GitCommandHandlerInterface> $handlers
30
         */
31
        public function __construct(string $file, array $handlers = [], string $source = self::SOURCE_EXPORT)
1✔
32
        {
33
                $this->file = $file;
1✔
34
                $this->source = $source;
1✔
35

36
                parent::__construct($handlers);
1✔
37
        }
1✔
38

39
        public static function createDefault(string $file): self
40
        {
41
                return new self($file, [
×
42
                        GetHeadCommand::class => new GetHeadCommandHandler(),
×
43
                        GetLatestTagCommand::class => new GetLatestTagCommandHandler(),
×
44
                ]);
45
        }
46

47
        public function getSource(): string
48
        {
49
                return $this->source;
1✔
50
        }
51

52
        public function isAccessible(): bool
53
        {
54
                return $this->isValid();
1✔
55
        }
56

57
        public function addHandler(string $commandClassname, GitCommandHandlerInterface $handler): void
1✔
58
        {
59
                if ($handler instanceof ExportedGitCommandHandlerInterface && $this->isValid()) {
1✔
60
                        $handler = $handler->withExportedValue($this->json ?? []);
1✔
61
                }
62

63
                parent::addHandler($commandClassname, $handler);
1✔
64
        }
1✔
65

66
        private function isValid(): bool
67
        {
68
                if (null !== $this->valid) {
1✔
69
                        return $this->valid;
1✔
70
                }
71

72
                if (!is_readable($this->file)) {
1✔
73
                        return $this->valid = false;
1✔
74
                }
75

76
                $content = @file_get_contents($this->file);
1✔
77

78
                if (false === $content) {
1✔
79
                        return $this->valid = false;
×
80
                }
81

82
                try {
83
                        $this->json = (array) json_decode($content, true, 512, JSON_THROW_ON_ERROR);
1✔
84

85
                        return $this->valid = true;
1✔
86
                } catch (JsonException $e) {
×
87
                        return $this->valid = false;
×
88
                }
89
        }
90
}
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