The code of constants only support int and string. (#2031)

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
沈唁 2020-07-04 13:15:04 +08:00 committed by GitHub
parent 29bf7fa58e
commit 12fda742ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 1 deletions

View File

@ -13,6 +13,10 @@
- [#2049](https://github.com/hyperf/hyperf/pull/2049) Optimized stdout when server restart for `hyperf/watcher`. - [#2049](https://github.com/hyperf/hyperf/pull/2049) Optimized stdout when server restart for `hyperf/watcher`.
## Changed
- [#2301](https://github.com/hyperf/hyperf/pull/2031) The code of constants only support `int` and `string`.
# v2.0.1 - 2020-07-02 # v2.0.1 - 2020-07-02
## Added ## Added

View File

@ -23,7 +23,8 @@ class AnnotationReader
foreach ($classConstants as $classConstant) { foreach ($classConstants as $classConstant) {
$code = $classConstant->getValue(); $code = $classConstant->getValue();
$docComment = $classConstant->getDocComment(); $docComment = $classConstant->getDocComment();
if ($docComment) { // Not support float and bool, because it will be convert to int.
if ($docComment && (is_int($code) || is_string($code))) {
$result[$code] = $this->parse($docComment, $result[$code] ?? []); $result[$code] = $this->parse($docComment, $result[$code] ?? []);
} }
} }

View File

@ -107,6 +107,15 @@ class AnnotationReaderTest extends TestCase
$this->assertSame('Type disabled', ErrorCodeStub::getType(ErrorCodeStub::TYPE_DISABLE)); $this->assertSame('Type disabled', ErrorCodeStub::getType(ErrorCodeStub::TYPE_DISABLE));
} }
public function testSupportTypes()
{
$container = $this->getContainer(true);
$this->assertSame('Type1001', ErrorCodeStub::getMessage(ErrorCodeStub::TYPE_INT));
$this->assertSame('', ErrorCodeStub::getMessage(ErrorCodeStub::TYPE_FLOAT));
$this->assertSame('Type1003.1', ErrorCodeStub::getMessage(ErrorCodeStub::TYPE_FLOAT_STRING));
$this->assertSame('TypeString', ErrorCodeStub::getMessage(ErrorCodeStub::TYPE_STRING));
}
protected function getContainer($has = false) protected function getContainer($has = false)
{ {
$container = Mockery::mock(ContainerInterface::class); $container = Mockery::mock(ContainerInterface::class);

View File

@ -62,4 +62,24 @@ class ErrorCodeStub extends AbstractConstants
* @Type("Type disabled") * @Type("Type disabled")
*/ */
const TYPE_DISABLE = 0; const TYPE_DISABLE = 0;
/**
* @Message("Type1001")
*/
const TYPE_INT = 1001;
/**
* @Message("Type1002.1")
*/
const TYPE_FLOAT = 1002.1;
/**
* @Message("Type1003.1")
*/
const TYPE_FLOAT_STRING = '1003.1';
/**
* @Message("TypeString")
*/
const TYPE_STRING = 'string';
} }