Skip to content

Commit

Permalink
Merge pull request #1431 from Nyholm/give-hint
Browse files Browse the repository at this point in the history
Add support for CodeLocationMissing to give a hint what code is missing
  • Loading branch information
Ocramius authored Jun 17, 2024
2 parents 10a3873 + d53940c commit 1e01cdb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/Reflection/Exception/CodeLocationMissing.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@

class CodeLocationMissing extends RuntimeException
{
public static function create(): self
public static function create(string|null $hint = null): self
{
return new self('Code location is missing');
$message = 'Code location is missing';
if ($hint !== null) {
$message .= '. ' . $hint;
}

return new self($message);
}
}
8 changes: 4 additions & 4 deletions src/Reflection/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public function getDefaultValueConstantName(): string
public function getStartLine(): int
{
if ($this->startLine === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for parameter "$%s".', $this->name));
}

return $this->startLine;
Expand All @@ -501,7 +501,7 @@ public function getStartLine(): int
public function getEndLine(): int
{
if ($this->endLine === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for parameter "$%s".', $this->name));
}

return $this->endLine;
Expand All @@ -515,7 +515,7 @@ public function getEndLine(): int
public function getStartColumn(): int
{
if ($this->startColumn === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for parameter "$%s".', $this->name));
}

return $this->startColumn;
Expand All @@ -529,7 +529,7 @@ public function getStartColumn(): int
public function getEndColumn(): int
{
if ($this->endColumn === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for parameter "$%s".', $this->name));
}

return $this->endColumn;
Expand Down
8 changes: 4 additions & 4 deletions src/Reflection/ReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function isDeprecated(): bool
public function getStartLine(): int
{
if ($this->startLine === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for property "$%s" in "%s".', $this->name, $this->implementingClass->getName()));
}

return $this->startLine;
Expand All @@ -383,7 +383,7 @@ public function getStartLine(): int
public function getEndLine(): int
{
if ($this->endLine === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for property "$%s" in "%s".', $this->name, $this->implementingClass->getName()));
}

return $this->endLine;
Expand All @@ -397,7 +397,7 @@ public function getEndLine(): int
public function getStartColumn(): int
{
if ($this->startColumn === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for property "$%s" in "%s".', $this->name, $this->implementingClass->getName()));
}

return $this->startColumn;
Expand All @@ -411,7 +411,7 @@ public function getStartColumn(): int
public function getEndColumn(): int
{
if ($this->endColumn === null) {
throw CodeLocationMissing::create();
throw CodeLocationMissing::create(sprintf('Was looking for property "$%s" in "%s".', $this->name, $this->implementingClass->getName()));
}

return $this->endColumn;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/Reflection/Exception/CodeLocationMissingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ public function testCreate(): void
self::assertInstanceOf(CodeLocationMissing::class, $exception);
self::assertSame('Code location is missing', $exception->getMessage());
}

public function testCreateWithHint(): void
{
$exception = CodeLocationMissing::create('Foobar');

self::assertInstanceOf(CodeLocationMissing::class, $exception);
self::assertSame('Code location is missing. Foobar', $exception->getMessage());
}
}

0 comments on commit 1e01cdb

Please sign in to comment.