Conditions | 17 |
Paths | 65 |
Total Lines | 57 |
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 |
||
46 | public function index() |
||
|
|||
47 | { |
||
48 | XMLDataFormatter::$api_base = 'api/ecommerce/v1/'; |
||
49 | if (!isset($this->urlParams['ClassName'])) { |
||
50 | return $this->notFound(); |
||
51 | } |
||
52 | $className = $this->urlParams['ClassName']; |
||
53 | $id = (isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null; |
||
54 | $relation = (isset($this->urlParams['Relation'])) ? $this->urlParams['Relation'] : null; |
||
55 | |||
56 | // Check input formats |
||
57 | if (!class_exists($className)) { |
||
58 | return $this->notFound(); |
||
59 | } |
||
60 | if ($id && !is_numeric($id)) { |
||
61 | return $this->notFound(); |
||
62 | } |
||
63 | if ($relation && !preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $relation)) { |
||
64 | return $this->notFound(); |
||
65 | } |
||
66 | |||
67 | // fix |
||
68 | if ($id) { |
||
69 | $obj = $className::get()->byID($id); |
||
70 | if ($obj) { |
||
71 | $className = $this->urlParams['ClassName'] = $obj->ClassName; |
||
72 | } else { |
||
73 | return $this->notFound(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | // if api access is disabled, don't proceed |
||
78 | $apiAccess = singleton($className)->stat('api_access'); |
||
79 | if (!$apiAccess) { |
||
80 | return $this->permissionFailure(); |
||
81 | } |
||
82 | |||
83 | // authenticate through HTTP BasicAuth |
||
84 | $this->member = $this->authenticate(); |
||
85 | |||
86 | // handle different HTTP verbs |
||
87 | if ($this->request->isGET() || $this->request->isHEAD()) { |
||
88 | return $this->getHandler($className, $id, $relation); |
||
89 | } |
||
90 | if ($this->request->isPOST()) { |
||
91 | return $this->postHandler($className, $id, $relation); |
||
92 | } |
||
93 | if ($this->request->isPUT()) { |
||
94 | return $this->putHandler($className, $id, $relation); |
||
95 | } |
||
96 | if ($this->request->isDELETE()) { |
||
97 | return $this->deleteHandler($className, $id, $relation); |
||
98 | } |
||
99 | |||
100 | // if no HTTP verb matches, return error |
||
101 | return $this->methodNotAllowed(); |
||
102 | } |
||
103 | } |
||
104 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.