Conditions | 4 |
Paths | 8 |
Total Lines | 120 |
Code Lines | 88 |
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 |
||
18 | public function testUpdateAccessToken() |
||
19 | { |
||
20 | $validToken = |
||
21 | '{"user":{"username":"somardesignstudios","bio":"","website":"","profile_picture":"",' . |
||
22 | '"full_name":"","id":"12345678"},"access_token":"","resource_owner_id":""}'; |
||
23 | |||
24 | $invalidToken = |
||
25 | '{"user":{"username":"somardesignstudios","bio":"","website":"","profile_picture":"",' . |
||
26 | '"full_name":"","id":"87654321"},"access_token":"","resource_owner_id":""}'; |
||
27 | |||
28 | /* |
||
29 | * Should throw if the session state is invalid. |
||
30 | */ |
||
31 | $mock = $this->getNewInstagramAccountMock()->makePartial(); |
||
32 | $mock |
||
33 | ->shouldReceive('getSessionOAuthState') |
||
34 | ->once() |
||
35 | ->withNoArgs() |
||
36 | ->andReturn('state'); |
||
37 | |||
38 | try { |
||
39 | $mock->updateAccessToken($validToken, 'invalidState'); |
||
40 | } catch (Exception $e) { |
||
41 | $this->assertEquals('Trying to set token on wrong InstagramAccount', $e->getMessage()); |
||
42 | } |
||
43 | |||
44 | /* |
||
45 | * Should throw if the session state is valid and the token username doesn't match the title. |
||
46 | */ |
||
47 | $mock = $this->getNewInstagramAccountMock()->makePartial(); |
||
48 | $mock |
||
49 | ->shouldReceive('getSessionOAuthState') |
||
50 | ->once() |
||
51 | ->withNoArgs() |
||
52 | ->andReturn('state'); |
||
53 | $mock |
||
54 | ->shouldReceive('getField') |
||
55 | ->once() |
||
56 | ->with('Title') |
||
57 | ->andReturn('otherusername'); |
||
58 | |||
59 | try { |
||
60 | $mock->updateAccessToken($validToken, 'state'); |
||
61 | } catch (Exception $e) { |
||
62 | $this->assertEquals('Trying to set token on wrong InstagramAccount', $e->getMessage()); |
||
63 | } |
||
64 | |||
65 | /* |
||
66 | * Should set the token if session state is valid and there's no current token. |
||
67 | */ |
||
68 | $mock = $this->getNewInstagramAccountMock()->makePartial(); |
||
69 | $mock |
||
70 | ->shouldReceive('getSessionOAuthState') |
||
71 | ->once() |
||
72 | ->withNoArgs() |
||
73 | ->andReturn('state'); |
||
74 | $mock |
||
75 | ->shouldReceive('getField') |
||
76 | ->with('Title') |
||
77 | ->andReturn('somardesignstudios'); |
||
78 | $mock |
||
79 | ->shouldReceive('getField') |
||
80 | ->with('AccessToken') |
||
81 | ->andReturn(null); |
||
82 | $mock |
||
83 | ->shouldReceive('setField') |
||
84 | ->once() |
||
85 | ->with('AccessToken', $validToken); |
||
86 | |||
87 | $mock->updateAccessToken($validToken, 'state'); |
||
88 | |||
89 | /* |
||
90 | * Should throw if the session state is valid, the token username matches the title, |
||
91 | * and the new token is for another account. |
||
92 | */ |
||
93 | $mock = $this->getNewInstagramAccountMock()->makePartial(); |
||
94 | $mock |
||
95 | ->shouldReceive('getSessionOAuthState') |
||
96 | ->once() |
||
97 | ->withNoArgs() |
||
98 | ->andReturn('state'); |
||
99 | $mock |
||
100 | ->shouldReceive('getField') |
||
101 | ->with('Title') |
||
102 | ->andReturn('somardesignstudios'); |
||
103 | $mock |
||
104 | ->shouldReceive('getField') |
||
105 | ->with('AccessToken') |
||
106 | ->andReturn($validToken); |
||
107 | |||
108 | try { |
||
109 | $mock->updateAccessToken($invalidToken, 'state'); |
||
110 | } catch (Exception $e) { |
||
111 | $this->assertEquals('Trying to set token on wrong InstagramAccount', $e->getMessage()); |
||
112 | } |
||
113 | |||
114 | /* |
||
115 | * Should update the token if the session state is valid and the token IDs match. |
||
116 | */ |
||
117 | $mock = $this->getNewInstagramAccountMock()->makePartial(); |
||
118 | $mock |
||
119 | ->shouldReceive('getSessionOAuthState') |
||
120 | ->once() |
||
121 | ->withNoArgs() |
||
122 | ->andReturn('state'); |
||
123 | $mock |
||
124 | ->shouldReceive('getField') |
||
125 | ->with('Title') |
||
126 | ->andReturn('somardesignstudios'); |
||
127 | $mock |
||
128 | ->shouldReceive('getField') |
||
129 | ->with('AccessToken') |
||
130 | ->andReturn($validToken); |
||
131 | $mock |
||
132 | ->shouldReceive('setField') |
||
133 | ->once() |
||
134 | ->with('AccessToken', $validToken); |
||
135 | |||
136 | $mock->updateAccessToken($validToken, 'state'); |
||
137 | } |
||
138 | |||
258 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.