Skip to content

Commit 9c1a8d8

Browse files
authored
Optimized error message when does not set the value of @var for @Inject. (#2429)
1 parent caf584a commit 9c1a8d8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/Annotation/Inject.php

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public function collectProperty(string $className, ?string $target): void
7474
throw $exception;
7575
}
7676
$this->value = '';
77+
} catch (\Throwable $exception) {
78+
throw new AnnotationException("The @Inject value is invalid for {$className}->{$target}. Because {$exception->getMessage()}");
7779
}
7880
}
7981
}

tests/InjectTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@
1313

1414
use Hyperf\Contract\ContainerInterface;
1515
use Hyperf\Di\Annotation\AnnotationReader;
16+
use Hyperf\Di\Annotation\Inject;
1617
use Hyperf\Di\Annotation\ScanConfig;
1718
use Hyperf\Di\Annotation\Scanner;
1819
use Hyperf\Di\Aop\Ast;
1920
use Hyperf\Di\BetterReflectionManager;
2021
use Hyperf\Di\ClassLoader;
22+
use Hyperf\Di\Exception\AnnotationException;
2123
use Hyperf\Di\Exception\NotFoundException;
2224
use Hyperf\Utils\ApplicationContext;
2325
use HyperfTest\Di\ExceptionStub\DemoInjectException;
2426
use HyperfTest\Di\Stub\AnnotationCollector;
2527
use HyperfTest\Di\Stub\AspectCollector;
2628
use HyperfTest\Di\Stub\Demo;
2729
use HyperfTest\Di\Stub\DemoInject;
30+
use HyperfTest\Di\Stub\EmptyVarValue;
2831
use Mockery;
2932
use PHPUnit\Framework\TestCase;
3033

@@ -76,6 +79,26 @@ public function testInjectException()
7679
}
7780
}
7881

82+
public function testInjectNotInitReflector()
83+
{
84+
$this->expectException(AnnotationException::class);
85+
$this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo. Because The class reflector object does not init yet');
86+
87+
$inject = new Inject();
88+
$inject->collectProperty(EmptyVarValue::class, 'demo');
89+
}
90+
91+
public function testInjectEmptyVar()
92+
{
93+
$this->expectException(AnnotationException::class);
94+
$this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo. Because Argument 1 passed to Roave\BetterReflection\TypesFinder\FindPropertyType::Roave\BetterReflection\TypesFinder\{closure}() must be an instance of phpDocumentor\Reflection\DocBlock\Tags\Var_, instance of phpDocumentor\Reflection\DocBlock\Tags\InvalidTag given');
95+
96+
BetterReflectionManager::initClassReflector([__DIR__ . '/Stub']);
97+
98+
$inject = new Inject();
99+
$inject->collectProperty(EmptyVarValue::class, 'demo');
100+
}
101+
79102
protected function getContainer()
80103
{
81104
$container = Mockery::mock(ContainerInterface::class);

tests/Stub/EmptyVarValue.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Di\Stub;
13+
14+
use Hyperf\Di\Annotation\Inject;
15+
16+
class EmptyVarValue
17+
{
18+
/**
19+
* @Inject
20+
* @var
21+
*/
22+
private $demo;
23+
}

0 commit comments

Comments
 (0)