Skip to content

Commit

Permalink
Optimized error message when does not set the value of @var for @Inject
Browse files Browse the repository at this point in the history
…. (#2429)
  • Loading branch information
limingxinleo committed Sep 3, 2020
1 parent e919359 commit cb66402
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-2.0.md
Expand Up @@ -5,6 +5,10 @@
- [#2411](https://github.com/hyperf/hyperf/pull/2411) Added method `Hyperf\Database\Query\Builder::forPageBeforeId` for database.
- [#2420](https://github.com/hyperf/hyperf/pull/2420) [#2426](https://github.com/hyperf/hyperf/pull/2426) Added option `enable-event-dispatcher` to initialize EventDispatcher for command.

## Optimized

- [#2429](https://github.com/hyperf/hyperf/pull/2429) Optimized error message when does not set the value of `@var` for `@Inject`.

# v2.0.9 - 2020-08-31

## Added
Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/db/gen.md
Expand Up @@ -244,7 +244,7 @@ class ModelUpdateVisitor extends Visitor
}

if (Str::startsWith($cast, 'decimal')) {
// 如果 cast 为 decimal,则这是 @property string
// 如果 cast 为 decimal, @property 改为 string
return 'string';
}

Expand Down
5 changes: 3 additions & 2 deletions docs/zh-cn/db/quick-start.md
Expand Up @@ -25,15 +25,16 @@ composer require hyperf/database
默认配置如下,数据库支持多库配置,默认为 `default`

| 配置项 | 类型 | 默认值 | 备注 |
|:--------------------:|:------:|:---------------:|:------------------:|
| :------------------: | :----: | :-------------: | :----------------: |
| driver | string || 数据库引擎 |
| host | string || 数据库地址 |
| database | string || 数据库默认 DB |
| database | string || 数据库默认 DB |
| username | string || 数据库用户名 |
| password | string | null | 数据库密码 |
| charset | string | utf8 | 数据库编码 |
| collation | string | utf8_unicode_ci | 数据库编码 |
| prefix | string | '' | 数据库模型前缀 |
| timezone | string | null | 数据库时区 |
| pool.min_connections | int | 1 | 连接池内最少连接数 |
| pool.max_connections | int | 10 | 连接池内最大连接数 |
| pool.connect_timeout | float | 10.0 | 连接等待超时时间 |
Expand Down
2 changes: 2 additions & 0 deletions src/di/src/Annotation/Inject.php
Expand Up @@ -74,6 +74,8 @@ public function collectProperty(string $className, ?string $target): void
throw $exception;
}
$this->value = '';
} catch (\Throwable $exception) {
throw new AnnotationException("The @Inject value is invalid for {$className}->{$target}. Because {$exception->getMessage()}");
}
}
}
23 changes: 23 additions & 0 deletions src/di/tests/InjectTest.php
Expand Up @@ -13,18 +13,21 @@

use Hyperf\Contract\ContainerInterface;
use Hyperf\Di\Annotation\AnnotationReader;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Di\Annotation\ScanConfig;
use Hyperf\Di\Annotation\Scanner;
use Hyperf\Di\Aop\Ast;
use Hyperf\Di\BetterReflectionManager;
use Hyperf\Di\ClassLoader;
use Hyperf\Di\Exception\AnnotationException;
use Hyperf\Di\Exception\NotFoundException;
use Hyperf\Utils\ApplicationContext;
use HyperfTest\Di\ExceptionStub\DemoInjectException;
use HyperfTest\Di\Stub\AnnotationCollector;
use HyperfTest\Di\Stub\AspectCollector;
use HyperfTest\Di\Stub\Demo;
use HyperfTest\Di\Stub\DemoInject;
use HyperfTest\Di\Stub\EmptyVarValue;
use Mockery;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -76,6 +79,26 @@ public function testInjectException()
}
}

public function testInjectNotInitReflector()
{
$this->expectException(AnnotationException::class);
$this->expectExceptionMessage('The @Inject value is invalid for HyperfTest\Di\Stub\EmptyVarValue->demo. Because The class reflector object does not init yet');

$inject = new Inject();
$inject->collectProperty(EmptyVarValue::class, 'demo');
}

public function testInjectEmptyVar()
{
$this->expectException(AnnotationException::class);
$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');

BetterReflectionManager::initClassReflector([__DIR__ . '/Stub']);

$inject = new Inject();
$inject->collectProperty(EmptyVarValue::class, 'demo');
}

protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
Expand Down
23 changes: 23 additions & 0 deletions src/di/tests/Stub/EmptyVarValue.php
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Di\Stub;

use Hyperf\Di\Annotation\Inject;

class EmptyVarValue
{
/**
* @Inject
* @var
*/
private $demo;
}

0 comments on commit cb66402

Please sign in to comment.