| Conditions | 6 |
| Paths | 1 |
| Total Lines | 84 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | protected function setUp() |
||
| 45 | { |
||
| 46 | $this->personType = new ObjectType([ |
||
|
|
|||
| 47 | 'name' => 'Person', |
||
| 48 | 'fields' => [ |
||
| 49 | 'firstName' => [ |
||
| 50 | 'type' => Type::string(), |
||
| 51 | 'phoneNumbers' => [ |
||
| 52 | 'type' => Type::listOf(Type::string()), |
||
| 53 | ], |
||
| 54 | ], |
||
| 55 | ], |
||
| 56 | ]); |
||
| 57 | |||
| 58 | $this->bookAttributesInputType = new InputObjectType([ |
||
| 59 | 'name' => 'BookAttributes', |
||
| 60 | 'fields' => [ |
||
| 61 | 'title' => [ |
||
| 62 | 'type' => Type::string(), |
||
| 63 | 'description' => 'Enter a book title, no more than 10 characters in length', |
||
| 64 | 'validate' => static function (string $title) { |
||
| 65 | if (strlen($title) > 10) { |
||
| 66 | return [1, 'book title must be less than 10 chaacters']; |
||
| 67 | } |
||
| 68 | return 0; |
||
| 69 | }, |
||
| 70 | ], |
||
| 71 | 'author' => [ |
||
| 72 | 'type' => Type::id(), |
||
| 73 | 'description' => 'Provide a valid author id', |
||
| 74 | 'errorCodes' => [ |
||
| 75 | 'unknownAuthor', |
||
| 76 | 'authorDeceased', |
||
| 77 | ], |
||
| 78 | 'validate' => function (string $authorId) { |
||
| 79 | if (! isset($this->data['people'][$authorId])) { |
||
| 80 | return ['unknownAuthor', 'We have no record of that author']; |
||
| 81 | } |
||
| 82 | return 0; |
||
| 83 | }, |
||
| 84 | ], |
||
| 85 | ], |
||
| 86 | ]); |
||
| 87 | |||
| 88 | $this->query = new ObjectType(['name' => 'Query']); |
||
| 89 | |||
| 90 | $this->schema = new Schema([ |
||
| 91 | 'query' => $this->query, |
||
| 92 | 'mutation' => new ObjectType([ |
||
| 93 | 'name' => 'Mutation', |
||
| 94 | 'fields' => function () { |
||
| 95 | return [ |
||
| 96 | 'updateBook' => new ValidatedFieldDefinition([ |
||
| 97 | 'name' => 'updateBook', |
||
| 98 | 'type' => Type::boolean(), |
||
| 99 | 'args' => [ |
||
| 100 | 'bookAttributes' => [ |
||
| 101 | 'type' => $this->bookAttributesInputType, |
||
| 102 | 'errorCodes' => ['titleOrIdRequired'], |
||
| 103 | 'validate' => static function (?array $bookAttributes) { |
||
| 104 | if (empty($bookAttributes)) { |
||
| 105 | return 0; |
||
| 106 | } |
||
| 107 | |||
| 108 | return isset($bookAttributes['title']) || isset($bookAttributes['author']) ? 0 : [ |
||
| 109 | 'titleOrIdRequired', |
||
| 110 | 'You must supply at least one of title or author', |
||
| 111 | ]; |
||
| 112 | }, |
||
| 113 | ], |
||
| 114 | ], |
||
| 115 | 'resolve' => static function ($value) : bool { |
||
| 116 | // ... |
||
| 117 | // do update |
||
| 118 | // ... |
||
| 119 | |||
| 120 | return ! $value; |
||
| 121 | }, |
||
| 122 | ]), |
||
| 123 | ]; |
||
| 124 | }, |
||
| 125 | ]), |
||
| 126 | ]); |
||
| 127 | } |
||
| 128 | |||
| 362 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..