| Conditions | 11 |
| Paths | 264 |
| Total Lines | 49 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 77 | public function execute() |
||
| 78 | { |
||
| 79 | try { |
||
| 80 | $response = array('status'=>null); |
||
| 81 | $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE); |
||
| 82 | $secretKey = $this->_request->getParam('secret'); |
||
| 83 | $privateKey = isset($this->config['pagantis_private_key']) ? $this->config['pagantis_private_key'] : null; |
||
| 84 | |||
| 85 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
| 86 | $dbConnection = $this->dbObject->getConnection(); |
||
| 87 | if ($privateKey != $secretKey) { |
||
| 88 | $response['status'] = 401; |
||
| 89 | $response['result'] = 'Unauthorized'; |
||
| 90 | } elseif ($this->_request->isPost()) { |
||
| 91 | if (count($_POST)) { |
||
| 92 | foreach ($_POST as $config => $value) { |
||
| 93 | if (isset($this->defaultConfigs[$config]) && $response['status']==null) { |
||
| 94 | $dbConnection->update( |
||
| 95 | $tableName, |
||
| 96 | array('value' => $value), |
||
| 97 | "config='$config'" |
||
| 98 | ); |
||
| 99 | } else { |
||
| 100 | $response['status'] = 400; |
||
| 101 | $response['result'] = 'Bad request'; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $response['status'] = 422; |
||
| 106 | $response['result'] = 'Empty data'; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | $formattedResult = array(); |
||
| 111 | if ($response['status']==null) { |
||
| 112 | $dbResult = $dbConnection->fetchAll("select * from $tableName"); |
||
| 113 | foreach ($dbResult as $value) { |
||
| 114 | $formattedResult[$value['config']] = $value['value']; |
||
| 115 | } |
||
| 116 | $response['result'] = $formattedResult; |
||
| 117 | } |
||
| 118 | $result = json_encode($response['result']); |
||
| 119 | header("HTTP/1.1 ".$response['status'], true, $response['status']); |
||
| 120 | header('Content-Type: application/json', true); |
||
| 121 | header('Content-Length: '.strlen($result)); |
||
| 122 | echo($result); |
||
| 123 | exit(); |
||
| 124 | } catch (\Exception $e) { |
||
| 125 | die($e->getMessage()); |
||
| 126 | } |
||
| 149 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: