Skip to content

Commit bc6b7cf

Browse files
authored
🎨 Added PHPStan strict rules and address reported issues (#80)
1 parent 5432564 commit bc6b7cf

24 files changed

+74
-55
lines changed

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"phpspec/prophecy-phpunit": "^2.0",
2323
"psr/http-client": "^1.0",
2424
"psr/log": "^2.0",
25-
"symfony/browser-kit": "^5.3",
25+
"symfony/browser-kit": "^5.3.1",
2626
"symfony/console": "^5.3",
2727
"symfony/css-selector": "^5.3",
2828
"symfony/dom-crawler": "^5.4",
@@ -38,6 +38,7 @@
3838
"mikey179/vfsstream": "^1.6.10",
3939
"phpstan/extension-installer": "^1.1",
4040
"phpstan/phpstan": "1.7.14",
41+
"phpstan/phpstan-strict-rules": "^1.2",
4142
"phpstan/phpstan-symfony": "^1",
4243
"phpunit/phpunit": "^9.5.20"
4344
},

‎phpstan.neon‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ parameters:
66
- ./tests
77
symfony:
88
console_application_loader: phpstan/console-application.php
9+
ignoreErrors:
10+
- '#Dynamic call to static method PHPUnit\\Framework\\.*#'

‎src/Commands/IssueTimelineCommand.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7878
$comments[$event->getComment()->id()][] = $event;
7979
}
8080

81-
foreach ($comments as $events) {
82-
$firstComment = reset($events)->getComment();
81+
foreach ($comments as $commentEvents) {
82+
$firstComment = reset($commentEvents)->getComment();
8383
$io->title(sprintf('<href=%s>Comment #%s (%d) at %s</>',
8484
$firstComment->url(),
8585
$firstComment->id(),
8686
$firstComment->getSequence(),
8787
$firstComment->getCreated()->format('r'),
8888
));
89-
foreach ($events as $event) {
90-
$io->text((string) $event);
89+
foreach ($commentEvents as $commentEvent) {
90+
$io->text((string) $commentEvent);
9191
}
9292
}
9393

‎src/Commands/Options/IssueMergeRequestOptions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function fromInput(InputInterface $input): static
3131
$instance->cookies = $input->getOption(static::OPTION_COOKIE);
3232
$directory = (string) $input->getArgument(static::ARGUMENT_DIRECTORY);
3333
$cwd = \getcwd();
34-
$instance->directory = ('.' === $directory && $cwd) ? $cwd : $directory;
34+
$instance->directory = ('.' === $directory && is_string($cwd)) ? $cwd : $directory;
3535
$instance->isHttp = $input->getOption(static::OPTION_HTTP);
3636

3737
$nid = $input->getArgument(static::ARGUMENT_ISSUE_ID);

‎src/Commands/Options/IssueTimelineCommandOptions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function fromInput(InputInterface $input): static
2222

2323
/** @var string $nid */
2424
$nid = $input->getArgument(static::ARGUMENT_ISSUE_ID);
25-
$instance->nid = preg_match('/^\d{1,10}$/m', $nid) ? (int) $nid : throw new \UnexpectedValueException('Issue ID is not valid');
25+
$instance->nid = (1 === preg_match('/^\d{1,10}$/m', $nid)) ? (int) $nid : throw new \UnexpectedValueException('Issue ID is not valid');
2626
$instance->noComments = (bool) $input->getOption(static::OPTION_NO_COMMENTS);
2727
$instance->noEvents = (bool) $input->getOption(static::OPTION_NO_EVENTS);
2828

‎src/Commands/Options/PatchToBranchOptions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function fromInput(InputInterface $input): static
6565

6666
// Check is a proper Composer constraint. e.g 8.8.x is not accepted:
6767
$constraint = (string) $input->getArgument(static::ARGUMENT_VERSION_CONSTRAINTS);
68-
if (!empty($constraint)) {
68+
if (strlen($constraint) > 0) {
6969
try {
7070
(new VersionParser())->parseConstraints($constraint);
7171
} catch (\UnexpectedValueException $e) {

‎src/Commands/Options/ProjectMergeRequestOptions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function fromInput(InputInterface $input): static
3333
$instance->branchName = $input->getOption(static::OPTION_BRANCH);
3434
$directory = (string) $input->getArgument(static::ARGUMENT_DIRECTORY);
3535
$cwd = \getcwd();
36-
$instance->directory = ('.' === $directory && $cwd) ? $cwd : $directory;
36+
$instance->directory = ('.' === $directory && is_string($cwd)) ? $cwd : $directory;
3737
$instance->project = $input->getArgument(static::ARGUMENT_PROJECT);
3838
$instance->isHttp = $input->getOption(static::OPTION_HTTP);
3939
$instance->includeAll = (bool) $input->getOption(static::OPTION_ALL);

‎src/Commands/ProjectCloneCommand.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5151

5252
$url = sprintf($options->isHttp ? 'https://git.drupalcode.org/project/%s.git' : '[email protected]:project/%s.git', $options->project);
5353
$params = [];
54-
if (!empty($options->branch)) {
54+
if (null !== $options->branch && strlen($options->branch) > 0) {
5555
$params['-b'] = $options->branch;
5656
}
5757

5858
// When directory isnt provided then use a directory with the same name as the project, if the directory
5959
// doesn't exist.
6060
$directory = $options->directory;
61-
if (!$directory) {
61+
if (null === $directory) {
6262
$directory = $options->project;
6363
}
6464

‎src/DrupalOrg/IssueGraph/DrupalOrgIssueGraph.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function graph(): \Generator
7474
yield new CommentEvent($commentStub);
7575

7676
// Detect merge requests.
77-
if (count($branchMrs) > 0 && $repoUrlGit && $repoUrlHttp) {
77+
if (count($branchMrs) > 0 && null !== $repoUrlGit && null !== $repoUrlHttp) {
7878
$fieldItems = $commentCrawler->filter('.field-items > *');
7979
foreach ($fieldItems as $fieldItem) {
8080
if (str_contains($fieldItem->textContent, 'opened merge request !')) {
@@ -172,7 +172,7 @@ private function findComments(Crawler $crawler): array
172172
foreach ($crawler->filter('#content .comments > .comment') as $commentElement) {
173173
assert($commentElement instanceof \DOMElement);
174174
$id = $commentElement->getAttribute('id');
175-
if (empty($id)) {
175+
if (0 === strlen($id)) {
176176
throw new \LogicException('All comments are expected to have an ID.');
177177
}
178178

‎src/DrupalOrg/Objects/DrupalOrgComment.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public function __construct(protected int $id)
2222

2323
public function getCreated(): \DateTimeImmutable
2424
{
25-
!$this->isStub ?: throw new \DomainException('Data missing for stubs.');
25+
if ($this->isStub) {
26+
throw new \DomainException('Data missing for stubs.');
27+
}
2628
$timestamp = $this->data->created ?? throw new \DomainException('Missing created date');
2729

2830
return new \DateTimeImmutable('@' . $timestamp);
@@ -67,7 +69,9 @@ public function getAuthorId(): int
6769

6870
public function getIssue(): DrupalOrgIssue
6971
{
70-
!$this->isStub ?: throw new \DomainException('Data missing for stubs.');
72+
if ($this->isStub) {
73+
throw new \DomainException('Data missing for stubs.');
74+
}
7175

7276
return $this->repository->share(DrupalOrgIssue::fromStub($this->data->node));
7377
}
@@ -88,7 +92,7 @@ public function getComment(): string
8892
{
8993
$commentBody = $this->data->comment_body ?? throw new \DomainException('Data missing for stubs.');
9094

91-
return !empty($commentBody->value) ? $commentBody->value : '';
95+
return isset($commentBody->value) && strlen($commentBody->value) > 0 ? $commentBody->value : '';
9296
}
9397

9498
public function importResponse(ResponseInterface $response): static

0 commit comments

Comments
 (0)