Skip to content

Commit 44b8262

Browse files
committed
Improve typing
1 parent a56bb27 commit 44b8262

11 files changed

Lines changed: 176 additions & 19 deletions

File tree

.phan/config.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* This configuration will be read and overlaid on top of the
5+
* default configuration. Command-line arguments will be applied
6+
* after this file is read.
7+
*/
8+
return [
9+
'target_php_version' => '7.3',
10+
'directory_list' => [
11+
'src',
12+
'vendor',
13+
],
14+
'exclude_file_regex' => '@^vendor/.*/(tests?|Tests?)/@',
15+
'exclude_analysis_directory_list' => [
16+
'vendor'
17+
],
18+
'plugins' => [
19+
'AlwaysReturnPlugin',
20+
'UnreachableCodePlugin',
21+
'DollarDollarPlugin',
22+
'DuplicateArrayKeyPlugin',
23+
'PregRegexCheckerPlugin',
24+
'PrintfCheckerPlugin',
25+
],
26+
];

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"require-dev": {
2929
"friendsofphp/php-cs-fixer": "^2.15.0",
30+
"phan/phan": "^2.3",
3031
"phpunit/phpunit": "^8.0",
3132
"phpmd/phpmd": "^2.7",
3233
"phpstan/phpstan": "^0.11.15",
@@ -51,11 +52,15 @@
5152
],
5253
"style-check": [
5354
"@phpcs",
55+
"@phpcsf",
5456
"@phpstan",
55-
"@phpmd"
57+
"@phpmd",
58+
"@psalm"
5659
],
5760
"phpunit": "phpunit --verbose",
58-
"phpcs": "php-cs-fixer fix -v --diff --dry-run",
61+
"phan": "phan",
62+
"phpcs": "phpcs",
63+
"phpcsf": "php-cs-fixer fix -v --diff --dry-run",
5964
"phpstan": "phpstan analyse src tests",
6065
"phpmd": "phpmd src text phpmd.xml",
6166
"psalm": "psalm"

src/Phug/Split.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
use Phug\Split\Command\Update;
99
use SimpleCli\SimpleCli;
1010

11+
/**
12+
* Class Split: main CLI program to manage split packages in a mono-repository.
13+
*
14+
* @psalm-suppress PropertyNotSetInConstructor
15+
*/
1116
class Split extends SimpleCli
1217
{
1318
public function getCommands(): array
@@ -20,12 +25,12 @@ public function getCommands(): array
2025
];
2126
}
2227

23-
public function gray()
28+
public function gray(): void
2429
{
2530
$this->write($this->getColorCode('dark_gray'));
2631
}
2732

28-
public function discolor()
33+
public function discolor(): void
2934
{
3035
$this->write($this->escapeCharacter.'[0m');
3136
}
@@ -44,7 +49,7 @@ public function warning(string $error): bool
4449
return false;
4550
}
4651

47-
public function chdir($directory): bool
52+
public function chdir(string $directory): bool
4853
{
4954
if ($directory !== getcwd()) {
5055
$this->writeLine("cd $directory", 'light_blue');

src/Phug/Split/Command/Analyze.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Analyze extends CommandBase
3232
/**
3333
* Last AST of projects.
3434
*
35-
* @var array[]
35+
* @var ?iterable<array>
3636
*/
3737
protected $ast;
3838

@@ -43,6 +43,8 @@ class Analyze extends CommandBase
4343
*/
4444
public function run(SimpleCli $cli): bool
4545
{
46+
$cli = $this->getSplitCli($cli);
47+
4648
return $this->calculatePackagesTree($cli) &&
4749
$this->dumpPackagesTree($cli, $this->getPackages());
4850
}
@@ -82,11 +84,12 @@ protected function getPackages(): iterable
8284
$this->ast = iterator_to_array($this->ast);
8385
}
8486

85-
return $this->ast;
87+
return $this->ast ?: [];
8688
}
8789

8890
protected function dumpPackagesTree(Split $cli, iterable $packages, int $level = 0): bool
8991
{
92+
/** @psalm-suppress InvalidArgument */
9093
$count = count($packages);
9194

9295
foreach ($packages as $index => $package) {

src/Phug/Split/Command/CommandBase.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,35 @@
33
namespace Phug\Split\Command;
44

55
use Exception;
6+
use Phug\Split;
67
use Phug\Split\Git\Commit;
78
use Phug\Split\Git\Log;
9+
use Phug\Split\InvalidCli;
810
use SimpleCli\CommandBase as Command;
11+
use SimpleCli\SimpleCli;
912

1013
/**
1114
* @property-read string $gitProgram Git program path.
1215
* @property-read string $hashPrefix Prefix for split linked commit hashes.
1316
*/
1417
abstract class CommandBase extends Command
1518
{
19+
/**
20+
* Assert the given CLI instance is a Split CLI instance.
21+
*
22+
* @param SimpleCli $cli
23+
*
24+
* @return Split
25+
*/
26+
protected function getSplitCli(SimpleCli $cli): Split
27+
{
28+
if ($cli instanceof Split) {
29+
return $cli;
30+
}
31+
32+
throw new InvalidCli($cli);
33+
}
34+
1635
/**
1736
* Escape a value for git command argument or option.
1837
*
@@ -69,11 +88,11 @@ protected function git(string $command, array $options = [], string $redirect =
6988
*
7089
* @suppressWarnings(PHPMD.StaticAccess)
7190
*
72-
* @return Log|Commit[]
91+
* @return Log
7392
*/
7493
protected function latest($count = 1, string $directory = ''): Log
7594
{
76-
return Log::fromGitLogString($this->git("log --pretty=fuller --max-count=$count $directory"));
95+
return Log::fromGitLogString($this->git("log --pretty=fuller --max-count=$count $directory") ?: '');
7796
}
7897

7998
/**

src/Phug/Split/Command/Copy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* Copy files from the mono-repository to a sub-package at the current linked revision.
13+
*
14+
* @psalm-suppress MissingConstructor
1315
*/
1416
class Copy extends CommandBase
1517
{
@@ -51,6 +53,8 @@ class Copy extends CommandBase
5153
*/
5254
public function run(SimpleCli $cli): bool
5355
{
56+
$cli = $this->getSplitCli($cli);
57+
5458
if (!$this->repository) {
5559
return $cli->error('Please provide an input repository URL.');
5660
}

src/Phug/Split/Command/Dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Dist extends Analyze
5050
*/
5151
public function run(SimpleCli $cli): bool
5252
{
53-
return $this->distribute($cli);
53+
return $this->distribute($this->getSplitCli($cli));
5454
}
5555

5656
protected function info(Split $cli, string $message): void

src/Phug/Split/Command/Update.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function getReplayLog(Split $cli, string $hash): Log
6060
$log = $this->latest($max, '.');
6161
$commits = [];
6262

63-
foreach ($log as $commit) {
63+
foreach ($log as /* @var Commit $commit */ $commit) {
6464
if ($commit->getHash() === $hash) {
6565
return new Log($commits);
6666
}
@@ -120,6 +120,23 @@ protected function getLocalUserConfig(): array
120120
return $localUser;
121121
}
122122

123+
/**
124+
* Set a git config.
125+
*
126+
* @param string $name
127+
* @param string|null $value
128+
*
129+
* @return string|null
130+
*/
131+
protected function setGitConfig(string $name, ?string $value): ?string
132+
{
133+
$config = $value === null
134+
? "--unset $name"
135+
: "$name ".$this->gitEscape($value);
136+
137+
return $this->git('config '.$config);
138+
}
139+
123140
/**
124141
* Apply the given commit in the current repository.
125142
*
@@ -169,10 +186,7 @@ protected function cherryPickCommit(
169186
$localUser = $this->getLocalUserConfig();
170187

171188
foreach (['name', 'email'] as $userConfig) {
172-
$config = empty($localUser[$userConfig])
173-
? "--unset user.$userConfig"
174-
: "user.$userConfig ".$this->gitEscape($localUser[$userConfig]);
175-
$this->git('config '.$config);
189+
$this->setGitConfig("user.$userConfig", $localUser[$userConfig] ?? null);
176190
}
177191
}
178192

src/Phug/Split/InvalidCli.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Phug\Split;
4+
5+
use InvalidArgumentException;
6+
use Phug\Split;
7+
use Throwable;
8+
9+
class InvalidCli extends InvalidArgumentException
10+
{
11+
public function __construct(object $cli, int $code = 0, Throwable $previous = null)
12+
{
13+
$class = get_class($cli);
14+
15+
parent::__construct("Invalid cli class given: $class, ".Split::class.' expected', $code, $previous);
16+
}
17+
}

tests/Phug/Split/Command/CommandBaseTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use Nette\Utils\FileSystem;
66
use PHPUnit\Framework\TestCase;
7+
use Phug\Split;
78
use Phug\Split\Command\CommandBase;
89
use Phug\Split\Command\Copy;
910
use Phug\Split\Git\Commit;
1011
use Phug\Split\Git\Log;
12+
use Phug\Split\InvalidCli;
1113
use ReflectionException;
1214
use ReflectionMethod;
15+
use SimpleCli\SimpleCli;
1316

1417
/**
1518
* @coversDefaultClass \Phug\Split\Command\CommandBase
@@ -233,4 +236,28 @@ public function testRemove()
233236

234237
$this->assertFalse($remove->invoke($copy, "//////\nnot/writable"));
235238
}
239+
240+
/**
241+
* @covers ::getSplitCli
242+
*/
243+
public function testCheckCliClass()
244+
{
245+
$copy = new Copy();
246+
$getSplitCli = new ReflectionMethod(CommandBase::class, 'getSplitCli');
247+
$getSplitCli->setAccessible(true);
248+
$split = new Split();
249+
$this->assertSame($split, $getSplitCli->invoke($copy, $split));
250+
}
251+
252+
/**
253+
* @covers ::getSplitCli
254+
* @covers \Phug\Split\InvalidCli::<public>
255+
*/
256+
public function testCheckCliClassException()
257+
{
258+
$this->expectException(InvalidCli::class);
259+
260+
(new Copy())->run(new class extends SimpleCli{
261+
});
262+
}
236263
}

0 commit comments

Comments
 (0)