| Total Complexity | 40 |
| Total Lines | 232 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
Complex classes like RequestProcessTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RequestProcessTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | trait RequestProcessTrait |
||
| 10 | { |
||
| 11 | abstract public function clearData(): bool; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param mixed $key Can be an array, a string, |
||
| 15 | * or a special formatted string |
||
| 16 | * (eg 'i18n/lang'). |
||
| 17 | * @param mixed $value The value to be stored. |
||
| 18 | * @return bool True on success and false on failure. |
||
| 19 | */ |
||
| 20 | abstract public function setData($key, $value): bool; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param mixed $key Can be an array, a string, |
||
| 24 | * or a special formatted string |
||
| 25 | * (eg 'i18n/lang'). |
||
| 26 | * @param mixed $defaultValue |
||
| 27 | * @return mixed |
||
| 28 | */ |
||
| 29 | abstract public function setting($key, $defaultValue = null); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Data value can be another array, for example _SERVER in CLI ("argv" is an array) |
||
| 33 | * Important: only first level is sanitized. |
||
| 34 | * |
||
| 35 | * @param array<string,array<int,string>|string> $data |
||
| 36 | * @return array<string,array<int,string>|string> |
||
| 37 | */ |
||
| 38 | public function sanitize(array $data): array |
||
| 39 | { |
||
| 40 | foreach ($data as $key => $value) { |
||
| 41 | if (\is_string($value)) { |
||
| 42 | // Sanitize only first level (prevents "argv" from being sanitized) |
||
| 43 | $value = \WebServCo\Framework\Helpers\RequestHelper::sanitizeString($value); |
||
| 44 | } |
||
| 45 | $data[$key] = $value; |
||
| 46 | } |
||
| 47 | return $data; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param array<string,mixed> $server |
||
| 52 | * @param array<string,string> $post |
||
| 53 | */ |
||
| 54 | protected function init(array $server, array $post = []): bool |
||
| 55 | { |
||
| 56 | $this->server = $this->sanitize($server); |
||
|
|
|||
| 57 | |||
| 58 | $this->setBody(); |
||
| 59 | $this->setMethod(); |
||
| 60 | $this->setFilename(); |
||
| 61 | $this->setPath(); |
||
| 62 | |||
| 63 | $this->process(); |
||
| 64 | |||
| 65 | switch ($this->method) { |
||
| 66 | case Method::GET: |
||
| 67 | case Method::HEAD: |
||
| 68 | break; |
||
| 69 | case Method::POST: |
||
| 70 | $this->processPost($post); |
||
| 71 | break; |
||
| 72 | } |
||
| 73 | if ($this->setting('clear_globals', false)) { |
||
| 74 | $this->clearGlobals(); |
||
| 75 | } |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | |||
| 79 | protected function setBody(): bool |
||
| 80 | { |
||
| 81 | $this->body = (string) \file_get_contents('php://input'); |
||
| 82 | return true; |
||
| 83 | } |
||
| 84 | |||
| 85 | protected function setMethod(): bool |
||
| 86 | { |
||
| 87 | if ( |
||
| 88 | empty($this->server['REQUEST_METHOD']) || |
||
| 89 | !\in_array( |
||
| 90 | $this->server['REQUEST_METHOD'], |
||
| 91 | Method::getSupported(), |
||
| 92 | true, |
||
| 93 | ) |
||
| 94 | ) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | $this->method = $this->server['REQUEST_METHOD']; |
||
| 98 | return true; |
||
| 99 | } |
||
| 100 | |||
| 101 | protected function setFilename(): bool |
||
| 102 | { |
||
| 103 | if (empty($this->server['SCRIPT_NAME'])) { |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | $this->filename = \basename($this->server['SCRIPT_NAME']); |
||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | protected function setPath(): bool |
||
| 111 | { |
||
| 112 | if (empty($this->server['SCRIPT_NAME'])) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->path = \rtrim( |
||
| 117 | \str_replace( |
||
| 118 | $this->filename, |
||
| 119 | '', |
||
| 120 | $this->server['SCRIPT_NAME'], |
||
| 121 | ), |
||
| 122 | \DIRECTORY_SEPARATOR, |
||
| 123 | ); |
||
| 124 | |||
| 125 | return true; |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function clearGlobals(): bool |
||
| 129 | { |
||
| 130 | if (!empty($_GET)) { |
||
| 131 | foreach ($_GET as $k => $v) { |
||
| 132 | unset($_REQUEST[$k]); |
||
| 133 | } |
||
| 134 | |||
| 135 | $_GET = []; |
||
| 136 | } |
||
| 137 | |||
| 138 | if (!empty($_POST)) { |
||
| 139 | $_POST = []; |
||
| 140 | } |
||
| 141 | return true; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param array<string,string> $post |
||
| 146 | */ |
||
| 147 | protected function processPost(array $post = []): bool |
||
| 154 | } |
||
| 155 | |||
| 156 | protected function process(): bool |
||
| 157 | { |
||
| 158 | if (\WebServCo\Framework\Helpers\PhpHelper::isCli()) { |
||
| 159 | return $this->processCli(); |
||
| 160 | } |
||
| 161 | |||
| 162 | return $this->processHttp(); |
||
| 163 | } |
||
| 164 | |||
| 165 | protected function processCli(): bool |
||
| 179 | } |
||
| 180 | |||
| 181 | protected function processHttp(): bool |
||
| 230 | } |
||
| 231 | |||
| 232 | private function getQueryString(): string |
||
| 244 |