Skip to content

Commit b918794

Browse files
committed
Process deprecated fields
1 parent 4fbcb61 commit b918794

File tree

10 files changed

+68
-24
lines changed

10 files changed

+68
-24
lines changed

src/SchemaGenerator/CodeGenerator/CodeFile/TraitFile.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ public function addProperty(string $name, $value = null)
9797

9898
/**
9999
* @param string $methodString
100+
* @param bool $isDeprecated
101+
* @param string|null $deprecationReason
100102
*/
101-
public function addMethod(string $methodString)
103+
public function addMethod(string $methodString, bool $isDeprecated = false, ?string $deprecationReason = null)
102104
{
103105
if (!empty($methodString)) {
104-
$this->methods[] = $methodString;
106+
$this->methods[] = $this->prependDeprecationComment($methodString, $isDeprecated, $deprecationReason);
105107
}
106108
}
107109

@@ -200,4 +202,23 @@ protected function serializeParameterValue($value): string
200202
{
201203
return StringLiteralFormatter::formatValueForRHS($value);
202204
}
205+
206+
/**
207+
* @param string $code
208+
* @param bool $isDeprecated
209+
* @param string|null $deprecationReason
210+
*
211+
* @return string
212+
*/
213+
protected function prependDeprecationComment(string $code, bool $isDeprecated, ?string $deprecationReason): string
214+
{
215+
if ($isDeprecated) {
216+
$code = "/**
217+
* @deprecated".($deprecationReason ? " $deprecationReason" : "")."
218+
*/
219+
".$code;
220+
}
221+
222+
return $code;
223+
}
203224
}

src/SchemaGenerator/CodeGenerator/QueryObjectClassBuilder.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,38 @@ public function __construct(string $writeDir, string $objectName, string $namesp
4141
/**
4242
* @param string $fieldName
4343
*/
44-
public function addScalarField(string $fieldName)
44+
public function addScalarField(string $fieldName, bool $isDeprecated, ?string $deprecationReason)
4545
{
4646
$upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName);
47-
$this->addSimpleSelector($fieldName, $upperCamelCaseProp);
47+
$this->addSimpleSelector($fieldName, $upperCamelCaseProp, $isDeprecated, $deprecationReason);
4848
}
4949

5050
/**
5151
* @param string $fieldName
5252
* @param string $typeName
5353
* @param string $argsObjectName
5454
*/
55-
public function addObjectField(string $fieldName, string $typeName, string $argsObjectName)
55+
public function addObjectField(string $fieldName, string $typeName, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason)
5656
{
5757
$upperCamelCaseProp = StringLiteralFormatter::formatUpperCamelCase($fieldName);
58-
$this->addObjectSelector($fieldName, $upperCamelCaseProp, $typeName, $argsObjectName);
58+
$this->addObjectSelector($fieldName, $upperCamelCaseProp, $typeName, $argsObjectName, $isDeprecated, $deprecationReason);
5959
}
6060

6161
/**
6262
* @param string $propertyName
6363
* @param string $upperCamelName
64+
* @param bool $isDeprecated
65+
* @param string|null $deprecationReason
6466
*/
65-
protected function addSimpleSelector(string $propertyName, string $upperCamelName)
67+
protected function addSimpleSelector(string $propertyName, string $upperCamelName, bool $isDeprecated, ?string $deprecationReason)
6668
{
6769
$method = "public function select$upperCamelName()
6870
{
6971
\$this->selectField(\"$propertyName\");
7072
7173
return \$this;
7274
}";
73-
$this->classFile->addMethod($method);
75+
$this->classFile->addMethod($method, $isDeprecated, $deprecationReason);
7476
}
7577

7678
/**
@@ -79,7 +81,7 @@ protected function addSimpleSelector(string $propertyName, string $upperCamelNam
7981
* @param string $fieldTypeName
8082
* @param string $argsObjectName
8183
*/
82-
protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $argsObjectName)
84+
protected function addObjectSelector(string $fieldName, string $upperCamelName, string $fieldTypeName, string $argsObjectName, bool $isDeprecated, ?string $deprecationReason)
8385
{
8486
$objectClassName = $fieldTypeName . 'QueryObject';
8587
$argsMapClassName = $argsObjectName . 'ArgumentsObject';
@@ -93,7 +95,7 @@ protected function addObjectSelector(string $fieldName, string $upperCamelName,
9395
9496
return \$object;
9597
}";
96-
$this->classFile->addMethod($method);
98+
$this->classFile->addMethod($method, $isDeprecated, $deprecationReason);
9799
}
98100

99101
/**
@@ -103,4 +105,4 @@ public function build(): void
103105
{
104106
$this->classFile->writeFile();
105107
}
106-
}
108+
}

src/SchemaGenerator/SchemaClassGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBui
9898
[$typeName, $typeKind] = $this->getTypeInfo($fieldArray);
9999

100100
if ($typeKind === FieldTypeKindEnum::SCALAR) {
101-
$queryObjectBuilder->addScalarField($name);
101+
$queryObjectBuilder->addScalarField($name, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']);
102102
} else {
103103

104104
// Generate nested type object if it wasn't generated
@@ -113,7 +113,7 @@ private function appendQueryObjectFields(QueryObjectClassBuilder $queryObjectBui
113113
if ($argsObjectGenerated) {
114114

115115
// Add sub type as a field to the query object if all generation happened successfully
116-
$queryObjectBuilder->addObjectField($name, $typeName, $argsObjectName);
116+
$queryObjectBuilder->addObjectField($name, $typeName, $argsObjectName, $fieldArray['isDeprecated'], $fieldArray['deprecationReason']);
117117
}
118118
}
119119
}
@@ -308,4 +308,4 @@ public function getWriteDir(): string
308308

309309
return $this->writeDir;
310310
}
311-
}
311+
}

src/SchemaGenerator/SchemaInspector.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public function getQueryTypeSchema(): array
6464
name
6565
kind
6666
description
67-
fields{
67+
fields(includeDeprecated: true){
6868
name
6969
description
70+
isDeprecated
71+
deprecationReason
7072
" . static::TYPE_SUB_QUERY . "
7173
args{
7274
name
@@ -94,9 +96,11 @@ public function getObjectSchema(string $objectName): array
9496
__type(name: \"$objectName\") {
9597
name
9698
kind
97-
fields{
99+
fields(includeDeprecated: true){
98100
name
99101
description
102+
isDeprecated
103+
deprecationReason
100104
" . static::TYPE_SUB_QUERY . "
101105
args{
102106
name

tests/QueryObjectClassBuilderTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testAddSimpleSelector()
6060
$objectName = 'SimpleSelector';
6161
$classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
6262
$objectName .= 'QueryObject';
63-
$classBuilder->addScalarField('name');
63+
$classBuilder->addScalarField('name', false, null);
6464
$classBuilder->build();
6565

6666
$this->assertFileEquals(
@@ -80,8 +80,8 @@ public function testAddMultipleSimpleSelectors()
8080
$objectName = 'MultipleSimpleSelectors';
8181
$classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
8282
$objectName .= 'QueryObject';
83-
$classBuilder->addScalarField('first_name');
84-
$classBuilder->addScalarField('last_name');
83+
$classBuilder->addScalarField('first_name', false, null);
84+
$classBuilder->addScalarField('last_name', true, 'is deprecated');
8585
$classBuilder->build();
8686

8787
$this->assertFileEquals(
@@ -99,7 +99,7 @@ public function testAddObjectSelector()
9999
$objectName = 'ObjectSelector';
100100
$classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
101101
$objectName .= 'QueryObject';
102-
$classBuilder->addObjectField('others', 'Other', 'RootOthers');
102+
$classBuilder->addObjectField('others', 'Other', 'RootOthers', false, null);
103103
$classBuilder->build();
104104

105105
$this->assertFileEquals(
@@ -119,13 +119,13 @@ public function testAddMultipleObjectSelectors()
119119
$objectName = 'MultipleObjectSelectors';
120120
$classBuilder = new QueryObjectClassBuilder(static::getGeneratedFilesDir(), $objectName, static::TESTING_NAMESPACE);
121121
$objectName .= 'QueryObject';
122-
$classBuilder->addObjectField('right_objects', 'Right', 'MultipleObjectSelectorsRightObjects');
123-
$classBuilder->addObjectField('left_objects', 'Left', 'MultipleObjectSelectorsLeftObjects');
122+
$classBuilder->addObjectField('right_objects', 'Right', 'MultipleObjectSelectorsRightObjects', false, null);
123+
$classBuilder->addObjectField('left_objects', 'Left', 'MultipleObjectSelectorsLeftObjects', true, null);
124124
$classBuilder->build();
125125

126126
$this->assertFileEquals(
127127
static::getExpectedFilesDir() . "/$objectName.php",
128128
static::getGeneratedFilesDir() . "/$objectName.php"
129129
);
130130
}
131-
}
131+
}

tests/SchemaClassGeneratorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ public function testGenerateQueryObjectWithScalarFields()
592592
[
593593
'name' => 'first_name',
594594
'description' => null,
595+
'isDeprecated' => false,
596+
'deprecationReason' => null,
595597
'type' => [
596598
'name' => 'String',
597599
'kind' => FieldTypeKindEnum::SCALAR,
@@ -602,6 +604,8 @@ public function testGenerateQueryObjectWithScalarFields()
602604
], [
603605
'name' => 'last_name',
604606
'description' => null,
607+
'isDeprecated' => true,
608+
'deprecationReason' => 'is deprecated',
605609
'type' => [
606610
'name' => 'String',
607611
'kind' => FieldTypeKindEnum::SCALAR,
@@ -641,6 +645,8 @@ public function testGenerateQueryObjectWithObjectFields()
641645
[
642646
'name' => 'right_objects',
643647
'description' => null,
648+
'isDeprecated' => false,
649+
'deprecationReason' => null,
644650
'type' => [
645651
'name' => null,
646652
'kind' => FieldTypeKindEnum::LIST,
@@ -656,6 +662,8 @@ public function testGenerateQueryObjectWithObjectFields()
656662
], [
657663
'name' => 'left_objects',
658664
'description' => null,
665+
'isDeprecated' => true,
666+
'deprecationReason' => null,
659667
'type' => [
660668
'name' => null,
661669
'kind' => FieldTypeKindEnum::LIST,

tests/TraitFileTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function testTraitWithMultipleMethods()
251251
print "test!";
252252
return 0;
253253
}'
254-
);
254+
, true, 'is deprecated');
255255
$trait->writeFile();
256256

257257
$this->assertFileEquals(static::getExpectedFilesDir() . "/$fileName.php" , $trait->getWritePath());

tests/files_expected/query_objects/MultipleObjectSelectorsQueryObject.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function selectRightObjects(MultipleObjectSelectorsRightObjectsArgumentsO
1919
return $object;
2020
}
2121

22+
/**
23+
* @deprecated
24+
*/
2225
public function selectLeftObjects(MultipleObjectSelectorsLeftObjectsArgumentsObject $argsObject = null)
2326
{
2427
$object = new LeftQueryObject("left_objects");

tests/files_expected/query_objects/MultipleSimpleSelectorsQueryObject.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public function selectFirstName()
1515
return $this;
1616
}
1717

18+
/**
19+
* @deprecated is deprecated
20+
*/
1821
public function selectLastName()
1922
{
2023
$this->selectField("last_name");

tests/files_expected/traits/TraitWithMultipleMethods.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public function testTheTrait() {
77
die();
88
}
99

10+
/**
11+
* @deprecated is deprecated
12+
*/
1013
private function innerTest() {
1114
print "test!";
1215
return 0;

0 commit comments

Comments
 (0)