Skip to content

Commit 15a08e7

Browse files
AmitMYchriskacerguis
authored andcommitted
Code smell, PHP Doc problems, Index methods, and global exception handler (chriskacerguis#726)
* Fixed throws statement * Removed unnecessary return doc * Fixed wrong return value * Fixed parameter types in PHP DOC * Removed paths inspection in the project * Added index methods support! Finally * Handle all exceptions showings in one place. not 2 * Added changes to the change log
1 parent 73eaa18 commit 15a08e7

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
2-
vendor
2+
vendor
3+
.idea

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog:
22
===========
33

4+
### UNRELEASED
5+
* Added support for CodeIgniter controller's index methods (index_GET, index_POST...)
6+
* Added exceptions handling when the method could not be found
7+
48
### 2.7.2
59

610
* Added $this->query() in which query parameters can now be obtained regardless of whether a GET request is sent or not

application/controllers/api/Example.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
defined('BASEPATH') OR exit('No direct script access allowed');
44

55
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
6+
/** @noinspection PhpIncludeInspection */
67
require APPPATH . '/libraries/REST_Controller.php';
78

89
/**

application/controllers/api/Key.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
defined('BASEPATH') OR exit('No direct script access allowed');
44

55
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
6+
/** @noinspection PhpIncludeInspection */
67
require APPPATH . '/libraries/REST_Controller.php';
78

89
/**

application/libraries/Format.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ public function to_php($data = NULL)
461461
// INTERNAL FUNCTIONS
462462

463463
/**
464-
* @param $data XML string
465-
* @return SimpleXMLElement XML element object; otherwise, empty array
464+
* @param string $data XML string
465+
* @return array XML element object; otherwise, empty array
466466
*/
467467
protected function _from_xml($data)
468468
{
@@ -496,7 +496,7 @@ protected function _from_csv($data, $delimiter = ',', $enclosure = '"')
496496
}
497497

498498
/**
499-
* @param $data Encoded json string
499+
* @param string $data Encoded json string
500500
* @return mixed Decoded json string with leading and trailing whitespace removed
501501
*/
502502
protected function _from_json($data)
@@ -505,7 +505,7 @@ protected function _from_json($data)
505505
}
506506

507507
/**
508-
* @param string Data to unserialized
508+
* @param string $data Data to unserialize
509509
* @return mixed Unserialized data
510510
*/
511511
protected function _from_serialize($data)
@@ -514,7 +514,7 @@ protected function _from_serialize($data)
514514
}
515515

516516
/**
517-
* @param $data Data to trim leading and trailing whitespace
517+
* @param string $data Data to trim leading and trailing whitespace
518518
* @return string Data with leading and trailing whitespace removed
519519
*/
520520
protected function _from_php($data)

application/libraries/REST_Controller.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ protected function early_checks()
375375
* @access public
376376
* @param string $config Configuration filename minus the file extension
377377
* e.g: my_rest.php is passed as 'my_rest'
378-
* @return void
379378
*/
380379
public function __construct($config = 'rest')
381380
{
@@ -590,7 +589,7 @@ public function __destruct()
590589
* Checks to see if we have everything we need to run this library.
591590
*
592591
* @access protected
593-
* @return Exception
592+
* @@throws Exception
594593
*/
595594
protected function preflight_checks()
596595
{
@@ -632,6 +631,11 @@ public function _remap($object_called, $arguments = [])
632631
$object_called = preg_replace('/^(.*)\.(?:'.implode('|', array_keys($this->_supported_formats)).')$/', '$1', $object_called);
633632

634633
$controller_method = $object_called.'_'.$this->request->method;
634+
// Does this method exist? If not, try executing an index method
635+
if (!method_exists($this, $controller_method)) {
636+
$controller_method = "index_" . $this->request->method;
637+
array_unshift($arguments, $object_called);
638+
}
635639

636640
// Do we want to log this method (if allowed by config)?
637641
$log_method = ! (isset($this->methods[$controller_method]['log']) && $this->methods[$controller_method]['log'] === FALSE);
@@ -725,13 +729,8 @@ public function _remap($object_called, $arguments = [])
725729
catch (Exception $ex)
726730
{
727731
// If the method doesn't exist, then the error will be caught and an error response shown
728-
$this->response([
729-
$this->config->item('rest_status_field_name') => FALSE,
730-
$this->config->item('rest_message_field_name') => [
731-
'classname' => get_class($ex),
732-
'message' => $ex->getMessage()
733-
]
734-
], self::HTTP_INTERNAL_SERVER_ERROR);
732+
$_error = &load_class('Exceptions', 'core');
733+
$_error->show_exception($ex);
735734
}
736735
}
737736

0 commit comments

Comments
 (0)