| Conditions | 11 |
| Paths | 365 |
| Total Lines | 47 |
| Code Lines | 33 |
| 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 |
||
| 56 | public function execute() |
||
| 57 | { |
||
| 58 | try { |
||
| 59 | $response = array(); |
||
| 60 | $secretKey = $this->getRequest()->getParam('secret'); |
||
| 61 | $privateKey = isset($this->config['pagantis_private_key']) ? $this->config['pagantis_private_key'] : null; |
||
| 62 | |||
| 63 | if ($secretKey!='' && $privateKey!='') { |
||
| 64 | $this->checkDbLogTable(); |
||
| 65 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
| 66 | $dbConnection = $this->dbObject->getConnection(); |
||
| 67 | $tableName = $this->dbObject->getTableName(self::LOGS_TABLE); |
||
| 68 | $sql = $dbConnection |
||
| 69 | ->select() |
||
| 70 | ->from($tableName, array('log', 'createdAt')); |
||
| 71 | |||
| 72 | if ($dateFrom = $this->getRequest()->getParam('from')) { |
||
| 73 | $sql->where('createdAt > ?', $dateFrom); |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($dateTo = $this->getRequest()->getParam('to')) { |
||
| 77 | $sql->where('createdAt < ?', $dateTo); |
||
| 78 | } |
||
| 79 | |||
| 80 | $limit = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 50; |
||
| 81 | $sql->limit($limit); |
||
| 82 | $sql->order('createdAt', 'desc'); |
||
| 83 | |||
| 84 | $results = $dbConnection->fetchAll($sql); |
||
| 85 | if (isset($results) && $privateKey == $secretKey) { |
||
| 86 | foreach ($results as $key => $result) { |
||
| 87 | $response[$key]['timestamp'] = $result['createdAt']; |
||
| 88 | $response[$key]['log'] = json_decode($result['log']); |
||
| 89 | } |
||
| 90 | } else { |
||
| 91 | $response['result'] = 'Error'; |
||
| 92 | } |
||
| 93 | |||
| 94 | $response = json_encode($response); |
||
| 95 | header("HTTP/1.1 200", true, 200); |
||
| 96 | header('Content-Type: application/json', true); |
||
| 97 | header('Content-Length: '.strlen($response)); |
||
| 98 | echo($response); |
||
| 99 | exit(); |
||
| 100 | } |
||
| 101 | } catch (\Exception $e) { |
||
| 102 | die($e->getMessage()); |
||
| 103 | } |
||
| 157 |
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: