1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Date: 07.12.15 |
4
|
|
|
* |
5
|
|
|
* @author Portey Vasil <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Youshido\Tests\StarWars\Schema; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class StarWarsData |
12
|
|
|
{ |
13
|
|
View Code Duplication |
private static function luke() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
return [ |
16
|
|
|
'id' => '1000', |
17
|
|
|
'name' => 'Luke Skywalker', |
18
|
|
|
'friends' => ['1002', '1003', '2000', '2001'], |
19
|
|
|
'appearsIn' => [4, 5, 6], |
20
|
|
|
'homePlanet' => 'Tatooine', |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
private static function vader() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
|
|
'id' => '1001', |
28
|
|
|
'name' => 'Darth Vader', |
29
|
|
|
'friends' => ['1004'], |
30
|
|
|
'appearsIn' => [4, 5, 6], |
31
|
|
|
'homePlanet' => 'Tatooine', |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
private static function han() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
return [ |
38
|
|
|
'id' => '1002', |
39
|
|
|
'name' => 'Han Solo', |
40
|
|
|
'friends' => ['1000', '1003', '2001'], |
41
|
|
|
'appearsIn' => [4, 5, 6], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
private static function leia() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'id' => '1003', |
49
|
|
|
'name' => 'Leia Organa', |
50
|
|
|
'friends' => ['1000', '1002', '2000', '2001'], |
51
|
|
|
'appearsIn' => [4, 5, 6], |
52
|
|
|
'homePlanet' => 'Alderaan', |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private static function tarkin() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
'id' => '1004', |
60
|
|
|
'name' => 'Wilhuff Tarkin', |
61
|
|
|
'friends' => ['1001'], |
62
|
|
|
'appearsIn' => [4], |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
static function humans() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'1000' => self::luke(), |
70
|
|
|
'1001' => self::vader(), |
71
|
|
|
'1002' => self::han(), |
72
|
|
|
'1003' => self::leia(), |
73
|
|
|
'1004' => self::tarkin(), |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
private static function threepio() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
'id' => '2000', |
81
|
|
|
'name' => 'C-3PO', |
82
|
|
|
'friends' => ['1000', '1002', '1003', '2001'], |
83
|
|
|
'appearsIn' => [4, 5, 6], |
84
|
|
|
'primaryFunction' => 'Protocol', |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* We export artoo directly because the schema returns him |
90
|
|
|
* from a root field, and hence needs to reference him. |
91
|
|
|
*/ |
92
|
|
|
static function artoo() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return [ |
95
|
|
|
|
96
|
|
|
'id' => '2001', |
97
|
|
|
'name' => 'R2-D2', |
98
|
|
|
'friends' => ['1000', '1002', '1003'], |
99
|
|
|
'appearsIn' => [4, 5, 6], |
100
|
|
|
'primaryFunction' => 'Astromech', |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
static function droids() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
'2000' => self::threepio(), |
108
|
|
|
'2001' => self::artoo(), |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Helper function to get a character by ID. |
114
|
|
|
*/ |
115
|
|
|
static function getCharacter($id) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$humans = self::humans(); |
118
|
|
|
$droids = self::droids(); |
119
|
|
|
if (isset($humans[$id])) { |
120
|
|
|
return $humans[$id]; |
121
|
|
|
} |
122
|
|
|
if (isset($droids[$id])) { |
123
|
|
|
return $droids[$id]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $episode |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
static function getHero($episode) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
if ($episode === 5) { |
136
|
|
|
// Luke is the hero of Episode V. |
137
|
|
|
return self::luke(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Artoo is the hero otherwise. |
141
|
|
|
return self::artoo(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Allows us to query for a character's friends. |
146
|
|
|
*/ |
147
|
|
|
static function getFriends($character) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
return array_map([__CLASS__, 'getCharacter'], $character['friends']); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.