Skip to content

Commit 46b10aa

Browse files
committed
Code tweaks
1 parent 5bc8511 commit 46b10aa

File tree

15 files changed

+37
-38
lines changed

15 files changed

+37
-38
lines changed

src/Naneau/ProjectVersioner/Reader/Composer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Composer
88
*
9-
* Finds version from composer lock file
9+
* Finds the version from composer lock file
1010
*/
1111
class Composer implements ReaderInterface
1212
{
@@ -21,7 +21,7 @@ public function canRead(string $directory): bool
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
public function read(string $directory)
24+
public function read(string $directory): int|string|null
2525
{
2626
$contents = file_get_contents($directory . '/composer.lock');
2727
if (!$contents) {

src/Naneau/ProjectVersioner/Reader/ComposerJson.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Naneau\ProjectVersioner\Reader;
33

4+
use JsonException;
45
use Naneau\ProjectVersioner\ReaderInterface;
56

67
use stdClass;
@@ -21,14 +22,19 @@ public function canRead(string $directory): bool
2122
/**
2223
* {@inheritdoc}
2324
*/
24-
public function read(string $directory)
25+
public function read(string $directory): int|string|null
2526
{
2627
$json = @file_get_contents($directory . '/composer.json');
2728
if (!$json) {
2829
return null;
2930
}
3031

31-
$json = json_decode($json, false);
32+
try {
33+
$json = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
34+
} catch (JsonException) {
35+
return null;
36+
}
37+
3238
if (!($json instanceof stdClass) || empty($json->version)) {
3339
return null;
3440
}

src/Naneau/ProjectVersioner/Reader/ComposerPackage.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Naneau\ProjectVersioner\Reader;
33

4+
use JsonException;
45
use Naneau\ProjectVersioner\ReaderInterface;
56

67
use stdClass;
@@ -15,10 +16,8 @@ class ComposerPackage implements ReaderInterface
1516
{
1617
/**
1718
* The page name to look for
18-
*
19-
* @var string
2019
*/
21-
private $package;
20+
private string $package;
2221

2322
public function __construct(string $package)
2423
{
@@ -46,7 +45,7 @@ public function canRead(string $directory): bool
4645
/**
4746
* {@inheritdoc}
4847
*/
49-
public function read(string $directory)
48+
public function read(string $directory): int|string|null
5049
{
5150
$package = $this->getPackageFromLockFile($directory);
5251

@@ -88,7 +87,12 @@ private function getPackageFromLockFile(string $directory): ?stdClass
8887
return null;
8988
}
9089

91-
$parsed = json_decode($contents, false);
90+
try {
91+
$parsed = json_decode($contents, false, 512, JSON_THROW_ON_ERROR);
92+
} catch (JsonException) {
93+
return null;
94+
}
95+
9296
if (!isset($parsed->packages)) {
9397
return null;
9498
}

src/Naneau/ProjectVersioner/Reader/File.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function canRead(string $directory): bool
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
public function read(string $directory)
36+
public function read(string $directory): int|string|null
3737
{
3838
$contents = file_get_contents($directory . DIRECTORY_SEPARATOR . $this->getFile());
3939
if (!$contents) {

src/Naneau/ProjectVersioner/Reader/Finder/Contents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Contents extends Finder
1111
/**
1212
* {@inheritdoc}
1313
*/
14-
public function read(string $directory)
14+
public function read(string $directory): string
1515
{
1616
$hash = '';
1717
foreach ($this->getFinder() as $file) {

src/Naneau/ProjectVersioner/Reader/Finder/Finder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
/**
99
* Finder
1010
*
11-
* Base class for finder based readers
11+
* Base class for finder-based readers
1212
*/
1313
abstract class Finder implements ReaderInterface
1414
{
1515
/**
1616
* the finder
17-
*
18-
* @var SfFinder
1917
*/
20-
private $finder;
18+
private SfFinder $finder;
2119

2220
public function __construct(?string $name = null, ?SfFinder $finder = null)
2321
{

src/Naneau/ProjectVersioner/Reader/Finder/MTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MTime extends Finder
1111
/**
1212
* {@inheritdoc}
1313
*/
14-
public function read(string $directory)
14+
public function read(string $directory): int|string|null
1515
{
1616
$highest = 0;
1717
foreach ($this->getFinder() as $file) {

src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ class Exec extends GitExec
1414
{
1515
/**
1616
* Use short hash?
17-
*
18-
* @var bool
1917
*/
20-
private $short = true;
18+
private bool $short = true;
2119

2220
public function __construct(bool $short = true)
2321
{

src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Reads the latest "described" version from git, which includes the latest
1010
* (reachable) tag, and a postfix for any commits added after that
1111
*
12-
* For example: 3.0.2-12-gd504031 for tag 3.0.2, with 12 additional commits,
12+
* For example, 3.0.2-12-gd504031 for tag 3.0.2, with 12 additional commits,
1313
* the latest being gd504031
1414
*
1515
* @see http://git-scm.com/docs/git-describe
@@ -18,9 +18,6 @@ class Exec extends GitExec
1818
{
1919
/**
2020
* Get command for directory
21-
*
22-
* @param string $directory
23-
* @return string
2421
*/
2522
protected function getCommandForDirectory(string $directory): string
2623
{

src/Naneau/ProjectVersioner/Reader/Git/Exec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Exec
1010
*
11-
* Base class for exec based git reading
11+
* Base class for exec-based git reading
1212
*/
1313
abstract class Exec implements ReaderInterface
1414
{
@@ -26,7 +26,7 @@ public function canRead(string $directory): bool
2626
/**
2727
* {@inheritDoc}
2828
*/
29-
public function read(string $directory)
29+
public function read(string $directory): int|string|null
3030
{
3131
return $this->exec(
3232
$this->getCommandForDirectory($directory)
@@ -63,7 +63,7 @@ private function canExec(string $command, string $directory): bool
6363
}
6464

6565
/**
66-
* Execute a git command and return first line of output
66+
* Execute a git command and return the first line of output
6767
*/
6868
private function exec(string $command): string
6969
{

0 commit comments

Comments
 (0)