From aeb66bb0b84387fd691d8c6fe8631611a314573d Mon Sep 17 00:00:00 2001 From: shalvah Date: Sat, 27 Aug 2022 20:15:27 +0200 Subject: [PATCH] Rename annotation attributes to fields --- .../Responses/UseApiResourceTags.php | 16 ++++----- .../Responses/UseResponseFileTag.php | 6 ++-- .../Strategies/Responses/UseResponseTag.php | 6 ++-- .../Responses/UseTransformerTags.php | 8 ++--- src/Tools/AnnotationParser.php | 30 ++++++++-------- .../Responses/UseResponseFileTagTest.php | 2 +- .../Responses/UseResponseTagTest.php | 2 +- tests/Unit/AnnotationParserTest.php | 36 +++++++++---------- 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/Extracting/Strategies/Responses/UseApiResourceTags.php b/src/Extracting/Strategies/Responses/UseApiResourceTags.php index 9e73eeed..42a008f9 100644 --- a/src/Extracting/Strategies/Responses/UseApiResourceTags.php +++ b/src/Extracting/Strategies/Responses/UseApiResourceTags.php @@ -74,11 +74,11 @@ private function getStatusCodeAndApiResourceClass(Tag $tag): array $status = $result[1] ?: 0; $content = $result[2]; - ['attributes' => $attributes, 'content' => $content] = a::parseIntoContentAndAttributes($content, ['status', 'scenario']); + ['fields' => $fields, 'content' => $content] = a::parseIntoContentAndFields($content, ['status', 'scenario']); - $status = $attributes['status'] ?: $status; + $status = $fields['status'] ?: $status; $apiResourceClass = $content; - $description = $attributes['scenario'] ? "$status, {$attributes['scenario']}" : "$status"; + $description = $fields['scenario'] ? "$status, {$fields['scenario']}" : "$status"; $isCollection = strtolower($tag->getName()) == 'apiresourcecollection'; return [(int)$status, $description, $apiResourceClass, $isCollection]; @@ -94,10 +94,10 @@ private function getClassToBeTransformedAndAttributes(array $tags): array $pagination = []; if ($modelTag) { - ['content' => $modelClass, 'attributes' => $attributes] = a::parseIntoContentAndAttributes($modelTag->getContent(), ['states', 'with', 'paginate']); - $states = $attributes['states'] ? explode(',', $attributes['states']) : []; - $relations = $attributes['with'] ? explode(',', $attributes['with']) : []; - $pagination = $attributes['paginate'] ? explode(',', $attributes['paginate']) : []; + ['content' => $modelClass, 'fields' => $fields] = a::parseIntoContentAndFields($modelTag->getContent(), ['states', 'with', 'paginate']); + $states = $fields['states'] ? explode(',', $fields['states']) : []; + $relations = $fields['with'] ? explode(',', $fields['with']) : []; + $pagination = $fields['paginate'] ? explode(',', $fields['paginate']) : []; } if (empty($modelClass)) { @@ -117,7 +117,7 @@ private function getClassToBeTransformedAndAttributes(array $tags): array private function getAdditionalData(array $tags): array { $tag = Arr::first(Utils::filterDocBlockTags($tags, 'apiresourceadditional')); - return $tag ? a::parseIntoAttributes($tag->getContent()) : []; + return $tag ? a::parseIntoFields($tag->getContent()) : []; } public function getApiResourceTag(array $tags): ?Tag diff --git a/src/Extracting/Strategies/Responses/UseResponseFileTag.php b/src/Extracting/Strategies/Responses/UseResponseFileTag.php index bcae1ceb..c376626f 100644 --- a/src/Extracting/Strategies/Responses/UseResponseFileTag.php +++ b/src/Extracting/Strategies/Responses/UseResponseFileTag.php @@ -35,10 +35,10 @@ public function getFileResponses(array $tags): ?array [$_, $status, $mainContent] = $result; $json = $result[3] ?? null; - ['attributes' => $attributes, 'content' => $filePath] = a::parseIntoContentAndAttributes($mainContent, ['status', 'scenario']); + ['fields' => $fields, 'content' => $filePath] = a::parseIntoContentAndFields($mainContent, ['status', 'scenario']); - $status = $attributes['status'] ?: ($status ?: 200); - $description = $attributes['scenario'] ? "$status, {$attributes['scenario']}" : "$status"; + $status = $fields['status'] ?: ($status ?: 200); + $description = $fields['scenario'] ? "$status, {$fields['scenario']}" : "$status"; $content = ResponseFileTools::getResponseContents($filePath, $json); return [ diff --git a/src/Extracting/Strategies/Responses/UseResponseTag.php b/src/Extracting/Strategies/Responses/UseResponseTag.php index fae70eef..cf9322a0 100644 --- a/src/Extracting/Strategies/Responses/UseResponseTag.php +++ b/src/Extracting/Strategies/Responses/UseResponseTag.php @@ -36,10 +36,10 @@ public function getDocBlockResponses(array $tags): ?array $status = $result[1] ?: 200; $content = $result[2] ?: '{}'; - ['attributes' => $attributes, 'content' => $content] = a::parseIntoContentAndAttributes($content, ['status', 'scenario']); + ['fields' => $fields, 'content' => $content] = a::parseIntoContentAndFields($content, ['status', 'scenario']); - $status = $attributes['status'] ?: $status; - $description = $attributes['scenario'] ? "$status, {$attributes['scenario']}" : "$status"; + $status = $fields['status'] ?: $status; + $description = $fields['scenario'] ? "$status, {$fields['scenario']}" : "$status"; return ['content' => $content, 'status' => (int) $status, 'description' => $description]; }, $responseTags); diff --git a/src/Extracting/Strategies/Responses/UseTransformerTags.php b/src/Extracting/Strategies/Responses/UseTransformerTags.php index 697b581b..48f3a7b0 100644 --- a/src/Extracting/Strategies/Responses/UseTransformerTags.php +++ b/src/Extracting/Strategies/Responses/UseTransformerTags.php @@ -87,10 +87,10 @@ private function getClassToBeTransformed(array $tags, ReflectionFunctionAbstract $relations = []; $resourceKey = null; if ($modelTag) { - ['content' => $type, 'attributes' => $attributes] = a::parseIntoContentAndAttributes($modelTag->getContent(), ['states', 'with', 'resourceKey']); - $states = $attributes['states'] ? explode(',', $attributes['states']) : []; - $relations = $attributes['with'] ? explode(',', $attributes['with']) : []; - $resourceKey = $attributes['resourceKey'] ?? null; + ['content' => $type, 'fields' => $fields] = a::parseIntoContentAndFields($modelTag->getContent(), ['states', 'with', 'resourceKey']); + $states = $fields['states'] ? explode(',', $fields['states']) : []; + $relations = $fields['with'] ? explode(',', $fields['with']) : []; + $resourceKey = $fields['resourceKey'] ?? null; } else { $parameter = Arr::first($transformerMethod->getParameters()); if ($parameter->hasType() && !$parameter->getType()->isBuiltin() && class_exists($parameter->getType()->getName())) { diff --git a/src/Tools/AnnotationParser.php b/src/Tools/AnnotationParser.php index 97856b96..194dc1cb 100644 --- a/src/Tools/AnnotationParser.php +++ b/src/Tools/AnnotationParser.php @@ -9,42 +9,42 @@ class AnnotationParser * Fields are always optional and may appear at the start or the end of the string. * * @param string $annotationContent - * @param array $allowedAttributes List of attributes to look for. + * @param array $allowedFields List of fields to look for. * - * @return array{content: string, attributes: string[]} + * @return array{content: string, fields: string[]} */ - public static function parseIntoContentAndAttributes(string $annotationContent, array $allowedAttributes): array + public static function parseIntoContentAndFields(string $annotationContent, array $allowedFields): array { - $parsedAttributes = array_fill_keys($allowedAttributes, null); + $parsedFields = array_fill_keys($allowedFields, null); - foreach ($allowedAttributes as $attribute) { - preg_match("/$attribute=([^\\s'\"]+|\".+?\"|'.+?')\\s*/", $annotationContent, $attributeAndValue); + foreach ($allowedFields as $field) { + preg_match("/$field=([^\\s'\"]+|\".+?\"|'.+?')\\s*/", $annotationContent, $fieldAndValue); - if (count($attributeAndValue)) { - [$matchingText, $attributeValue] = $attributeAndValue; + if (count($fieldAndValue)) { + [$matchingText, $attributeValue] = $fieldAndValue; $annotationContent = str_replace($matchingText, '', $annotationContent); - $parsedAttributes[$attribute] = trim($attributeValue, '"\' '); + $parsedFields[$field] = trim($attributeValue, '"\' '); } } return [ 'content' => trim($annotationContent), - 'attributes' => $parsedAttributes + 'fields' => $parsedFields ]; } /** * Parse an annotation like 'title=This message="everything good"' into a key-value array. * All non key-value fields will be ignored. Useful for `@apiResourceAdditional`, - * where users may specify arbitrary attributes. + * where users may specify arbitrary fields. * * @param string $annotationContent * @return array */ - public static function parseIntoAttributes(string $annotationContent): array + public static function parseIntoFields(string $annotationContent): array { - $attributes = $matches = []; + $fields = $matches = []; preg_match_all( '/([^\s\'"]+|".+?"|\'.+?\')=([^\s\'"]+|".+?"|\'.+?\')/', @@ -54,9 +54,9 @@ public static function parseIntoAttributes(string $annotationContent): array ); foreach ($matches as $match) { - $attributes[trim($match[1], '"\' ')] = trim($match[2], '"\' '); + $fields[trim($match[1], '"\' ')] = trim($match[2], '"\' '); } - return $attributes; + return $fields; } } diff --git a/tests/Strategies/Responses/UseResponseFileTagTest.php b/tests/Strategies/Responses/UseResponseFileTagTest.php index b53c0bf5..3b550d6a 100644 --- a/tests/Strategies/Responses/UseResponseFileTagTest.php +++ b/tests/Strategies/Responses/UseResponseFileTagTest.php @@ -60,7 +60,7 @@ public function responseFileTags() ], ], - "with attributes" => [ + "with fields" => [ [ new Tag('responseFile', 'scenario="success" tests/Fixtures/response_test.json'), new Tag('responseFile', 'status=401 scenario=\'auth problem\' tests/Fixtures/response_error_test.json'), diff --git a/tests/Strategies/Responses/UseResponseTagTest.php b/tests/Strategies/Responses/UseResponseTagTest.php index 398ed2ad..f3c08f00 100644 --- a/tests/Strategies/Responses/UseResponseTagTest.php +++ b/tests/Strategies/Responses/UseResponseTagTest.php @@ -63,7 +63,7 @@ public function responseTags() ], ], - "with attributes" => [ + "with fields" => [ [ new Tag('response', "scenario=\"success\" $response1"), new Tag('response', "status=401 scenario='auth problem' $response2"), diff --git a/tests/Unit/AnnotationParserTest.php b/tests/Unit/AnnotationParserTest.php index 043bd1e5..4f624bf4 100644 --- a/tests/Unit/AnnotationParserTest.php +++ b/tests/Unit/AnnotationParserTest.php @@ -9,43 +9,43 @@ class AnnotationParserTest extends TestCase { /** * @test - * @dataProvider annotationsWithContentAndAttributes + * @dataProvider annotationsWithContentAndFields */ - public function can_parse_annotation_into_content_and_attributes(string $annotation, array $expected) + public function can_parse_annotation_into_content_and_fields(string $annotation, array $expected) { - $result = AnnotationParser::parseIntoContentAndAttributes($annotation, ['status', 'scenario']); + $result = AnnotationParser::parseIntoContentAndFields($annotation, ['status', 'scenario']); $this->assertEquals($expected, $result); } - public function annotationsWithContentAndAttributes() + public function annotationsWithContentAndFields() { return [ - "when attributes come first" => [ + "when fields come first" => [ 'status=400 scenario="things go wrong" {"message": "failed"}', [ - 'attributes' => ['status' => '400', 'scenario' => 'things go wrong'], + 'fields' => ['status' => '400', 'scenario' => 'things go wrong'], 'content' => '{"message": "failed"}', ], ], - "when attributes come last" => [ + "when fields come last" => [ '{"message": "failed"} status=400 scenario="things go wrong"', [ - 'attributes' => ['status' => '400', 'scenario' => 'things go wrong'], + 'fields' => ['status' => '400', 'scenario' => 'things go wrong'], 'content' => '{"message": "failed"}', ], ], - "when there are no attributes" => [ + "when there are no fields" => [ '{"message": "failed"} ', [ - 'attributes' => ['status' => null, 'scenario' => null], + 'fields' => ['status' => null, 'scenario' => null], 'content' => '{"message": "failed"}', ], ], - "when there are some attributes" => [ + "when there are some fields" => [ ' status=hey {"message": "failed"} ', [ - 'attributes' => ['status' => 'hey', 'scenario' => null], + 'fields' => ['status' => 'hey', 'scenario' => null], 'content' => '{"message": "failed"}', ], ], @@ -54,16 +54,16 @@ public function annotationsWithContentAndAttributes() /** * @test - * @dataProvider annotationsWithAttributes + * @dataProvider annotationsWithFields */ - public function can_parse_annotation_into_attributes(string $annotation, array $expected) + public function can_parse_annotation_into_fields(string $annotation, array $expected) { - $result = AnnotationParser::parseIntoAttributes($annotation); + $result = AnnotationParser::parseIntoFields($annotation); $this->assertEquals($expected, $result); } - public function annotationsWithAttributes() + public function annotationsWithFields() { return [ "with or without quotes" => [ @@ -75,11 +75,11 @@ public function annotationsWithAttributes() 'snaked_data' => 'value' ] ], - "no attributes" => [ + "no fields" => [ '{"message": "failed"}', [] ], - "attributes with empty values" => [ + "fields with empty values" => [ 'title= message="everything good"', [ 'message' => 'everything good'