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

eiriksm / php-app-git-version / 5632650910

pending completion
5632650910

push

github

web-flow
Test PHP 8.3 (#35)

28 of 28 relevant lines covered (100.0%)

14.57 hits per line

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

100.0
/src/GitInfo.php
1
<?php
2

3
namespace eiriksm\GitInfo;
4

5
use Symfony\Component\Process\Process;
6

7
/**
8
 * Class GitInfo.
9
 */
10
class GitInfo implements GitInfoInterface {
11

12
  /**
13
   * The git command.
14
   *
15
   * @var string
16
   */
17
  private $gitCommand;
18

19
  /**
20
   * Constructor.
21
   */
22
  public function __construct($git_command = 'git') {
23
    $this->gitCommand = $git_command;
30✔
24
  }
25

26
  /**
27
   * {@inheritdoc}
28
   */
29
  public function getShortHash() {
30
    return $this->execAndTrim(['log', '--pretty=%h', '-n1', 'HEAD']);
12✔
31
  }
32

33
  /**
34
   * {@inheritdoc}
35
   */
36
  public function getVersion() {
37
    $tag = $this->execAndTrim(['describe', '--tags']);
12✔
38
    return !empty($tag) ? $tag : 'dev';
12✔
39
  }
40

41
  /**
42
   * Fetches the date from the git log.
43
   * 
44
   * @return \DateTime|null
45
   *   Time object in UTC or null if the date could not be fetched.
46
   */
47
  protected function getGitDate() {
48
    if (!$date = $this->execAndTrim(['log', '-n1', '--pretty=%ci', 'HEAD'])) {
18✔
49
      return NULL;
6✔
50
    }
51
    $commit_date = new \DateTime($date);
12✔
52
    $commit_date->setTimezone(new \DateTimeZone('UTC'));
12✔
53
    return $commit_date;
12✔
54
  }
55

56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function getDate() {
60
    @trigger_error('GitInfoInterface::getDate() is deprecated in eiriksm/gitinfo:4.1.0 and is removed from eiriksm/gitinfo:5.0.0. Use ::getRfc3339Date() or ::getCustomDate().', E_USER_DEPRECATED);
6✔
61
    return $this->getCustomDate('Y-m-d H:m:s') ?? FALSE;
6✔
62
  }
63

64
  /**
65
   * {@inheritdoc}
66
   */
67
  public function getRfc3339Date() {
68
    return $this->getCustomDate(\DateTimeInterface::RFC3339_EXTENDED);
3✔
69
  }
70

71
  /**
72
   * {@inheritdoc}
73
   */
74
  public function getCustomDate(string $format) {
75
    $date = $this->getGitDate();
18✔
76
    return $date ? $date->format($format) : NULL;
18✔
77
  }
78
  
79
  /**
80
   * Helper to make sure we trim the output.
81
   *
82
   * @param string $command
83
   *   The command to run.
84
   *
85
   * @return string|bool
86
   *   The output string, or FALSE if things went badly.
87
   */
88
  protected function execAndTrim(array $commands) {
89
    try {
90
      $process_command = array_merge([$this->gitCommand], $commands);
30✔
91
      $process = new Process($process_command);
30✔
92
      $exit_code = $process->run();
30✔
93
      if ($exit_code) {
30✔
94
        throw new \Exception('Process exited with exit code ' . $exit_code);
12✔
95
      }
96
      $result = trim($process->getOutput());
18✔
97
    }
98
    catch (\Exception $e) {
12✔
99
      $result = FALSE;
12✔
100
    }
101
    return $result;
30✔
102
  }
103

104
  /**
105
   * {@inheritdoc}
106
   */
107
  public function getApplicationVersionString() {
108
    $date = $this->getCustomDate('Y-m-d H:m:s');
6✔
109
    $date_string = '';
6✔
110
    if ($date) {
6✔
111
      $date_string = " ($date)";
3✔
112
    }
113
    return sprintf('v.%s.%s%s', $this->getVersion(), $this->getShortHash(), $date_string);
6✔
114
  }
115

116
}
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