diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index f5c9bc894..df9d32b6e 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -13,6 +13,10 @@ - [#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 ## Added diff --git a/src/constants/src/AnnotationReader.php b/src/constants/src/AnnotationReader.php index 2363d5c62..0b73f363d 100644 --- a/src/constants/src/AnnotationReader.php +++ b/src/constants/src/AnnotationReader.php @@ -23,7 +23,8 @@ class AnnotationReader foreach ($classConstants as $classConstant) { $code = $classConstant->getValue(); $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] ?? []); } } diff --git a/src/constants/tests/AnnotationReaderTest.php b/src/constants/tests/AnnotationReaderTest.php index aaa9054ab..7d1e09a6d 100644 --- a/src/constants/tests/AnnotationReaderTest.php +++ b/src/constants/tests/AnnotationReaderTest.php @@ -107,6 +107,15 @@ class AnnotationReaderTest extends TestCase $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) { $container = Mockery::mock(ContainerInterface::class); diff --git a/src/constants/tests/Stub/ErrorCodeStub.php b/src/constants/tests/Stub/ErrorCodeStub.php index 1b8c2f89b..5753fa204 100644 --- a/src/constants/tests/Stub/ErrorCodeStub.php +++ b/src/constants/tests/Stub/ErrorCodeStub.php @@ -62,4 +62,24 @@ class ErrorCodeStub extends AbstractConstants * @Type("Type disabled") */ 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'; }