From 0232d18f1fe22ffbe74e1d100b5661e41ec46e65 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 16 Feb 2017 14:30:06 -0500 Subject: [PATCH] Yet more snake case to camel case --- src/API/Kitsu.php | 8 +- src/Controller.php | 73 +++++++++-------- src/Controller/Anime.php | 10 +-- src/Controller/Collection.php | 6 +- src/Controller/Manga.php | 10 +-- src/Dispatcher.php | 81 ++++++++++--------- src/MenuGenerator.php | 10 +-- src/Util.php | 16 ++-- tests/MenuGeneratorTest.php | 2 +- tests/UtilTest.php | 12 +-- .../test_data/Kitsu/animeAfterTransform.json | 8 +- 11 files changed, 123 insertions(+), 113 deletions(-) diff --git a/src/API/Kitsu.php b/src/API/Kitsu.php index cc79be83..9ac7bb8c 100644 --- a/src/API/Kitsu.php +++ b/src/API/Kitsu.php @@ -110,21 +110,21 @@ class Kitsu { return [ 'name' => 'Crunchyroll', 'link' => true, - 'logo' => '' + 'image' => 'streaming-logos/crunchyroll.svg', ]; case 'www.funimation.com': return [ 'name' => 'Funimation', 'link' => true, - 'logo' => '' + 'image' => 'streaming-logos/funimation.svg', ]; case 'www.hulu.com': return [ 'name' => 'Hulu', 'link' => true, - 'logo' => '' + 'image' => 'streaming-logos/hulu.svg', ]; // Default to Netflix, because the API links are broken, @@ -133,7 +133,7 @@ class Kitsu { return [ 'name' => 'Netflix', 'link' => false, - 'logo' => '' + 'image' => 'streaming-logos/netflix.svg', ]; } } diff --git a/src/Controller.php b/src/Controller.php index 63e1194c..4f27026b 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -107,10 +107,10 @@ class Controller { $this->session = $session->getSegment(SESSION_SEGMENT); // Set a 'previous' flash value for better redirects - $server_params = $this->request->getServerParams(); - if (array_key_exists('HTTP_REFERER', $server_params)) + $serverParams = $this->request->getServerParams(); + if (array_key_exists('HTTP_REFERER', $serverParams)) { - $this->session->setFlash('previous', $server_params['HTTP_REFERER']); + $this->session->setFlash('previous', $serverParams['HTTP_REFERER']); } // Set a message box if available @@ -124,8 +124,8 @@ class Controller { */ public function redirectToDefaultRoute() { - $default_type = $this->config->get(['routes', 'route_config', 'default_list']); - $this->redirect($this->urlGenerator->defaultUrl($default_type), 303); + $defaultType = $this->config->get(['routes', 'route_config', 'default_list']); + $this->redirect($this->urlGenerator->defaultUrl($defaultType), 303); } /** @@ -133,7 +133,7 @@ class Controller { * * @return void */ - public function redirect_to_previous() + public function redirectToPrevious() { $previous = $this->session->getFlash('previous'); $this->redirect($previous, 303); @@ -145,31 +145,31 @@ class Controller { * @param string|null $url * @return void */ - public function set_session_redirect($url = NULL) + public function setSessionRedirect($url = NULL) { - $server_params = $this->request->getServerParams(); + $serverParams = $this->request->getServerParams(); - if ( ! array_key_exists('HTTP_REFERER', $server_params)) + if ( ! array_key_exists('HTTP_REFERER', $serverParams)) { return; } $util = $this->container->get('util'); - $double_form_page = $server_params['HTTP_REFERER'] === $this->request->getUri(); + $doubleFormPage = $serverParams['HTTP_REFERER'] === $this->request->getUri(); // Don't attempt to set the redirect url if // the page is one of the form type pages, // and the previous page is also a form type page_segments - if ($double_form_page) + if ($doubleFormPage) { return; } if (is_null($url)) { - $url = $util->is_view_page() + $url = $util->isViewPage() ? $this->request->url->get() - : $server_params['HTTP_REFERER']; + : $serverParams['HTTP_REFERER']; } $this->session->set('redirect_url', $url); @@ -180,7 +180,7 @@ class Controller { * * @return void */ - public function session_redirect() + public function sessionRedirect() { $target = $this->session->get('redirect_url'); if (empty($target)) @@ -221,7 +221,7 @@ class Controller { * @throws InvalidArgumentException * @return string */ - protected function load_partial($view, $template, array $data = []) + protected function loadPartial($view, $template, array $data = []) { $router = $this->container->get('dispatcher'); @@ -234,14 +234,14 @@ class Controller { $data['route_path'] = $route ? $router->getRoute()->path : ''; - $template_path = _dir($this->config->get('view_path'), "{$template}.php"); + $templatePath = _dir($this->config->get('view_path'), "{$template}.php"); - if ( ! is_file($template_path)) + if ( ! is_file($templatePath)) { throw new InvalidArgumentException("Invalid template : {$template}"); } - return $view->renderTemplate($template_path, (array)$data); + return $view->renderTemplate($templatePath, (array)$data); } /** @@ -252,17 +252,17 @@ class Controller { * @param array $data * @return void */ - protected function render_full_page($view, $template, array $data) + protected function renderFullPage($view, $template, array $data) { - $view->appendOutput($this->load_partial($view, 'header', $data)); + $view->appendOutput($this->loadPartial($view, 'header', $data)); if (array_key_exists('message', $data) && is_array($data['message'])) { - $view->appendOutput($this->load_partial($view, 'message', $data['message'])); + $view->appendOutput($this->loadPartial($view, 'message', $data['message'])); } - $view->appendOutput($this->load_partial($view, $template, $data)); - $view->appendOutput($this->load_partial($view, 'footer', $data)); + $view->appendOutput($this->loadPartial($view, $template, $data)); + $view->appendOutput($this->loadPartial($view, 'footer', $data)); } /** @@ -280,11 +280,11 @@ class Controller { if ($status !== '') { - $message = $this->show_message($view, 'error', $status); + $message = $this->showMessage($view, 'error', $status); } // Set the redirect url - $this->set_session_redirect(); + $this->setSessionRedirect(); $this->outputHTML('login', [ 'title' => 'Api login', @@ -303,10 +303,10 @@ class Controller { $post = $this->request->getParsedBody(); if ($auth->authenticate($post['password'])) { - return $this->session_redirect(); + return $this->sessionRedirect(); } - $this->set_flash_message('Invalid username or password.'); + $this->setFlashMessage('Invalid username or password.'); $this->redirect($this->urlGenerator->url('login'), 303); } @@ -361,12 +361,21 @@ class Controller { * @param string $type * @return void */ - public function set_flash_message($message, $type = "info") + public function setFlashMessage($message, $type = "info") { - $this->session->setFlash('message', [ + static $messages; + + if (!$messages) + { + $messages = []; + } + + $messages[] = [ 'message_type' => $type, 'message' => $message - ]); + ]; + + $this->session->setFlash('message', $messages); } /** @@ -393,7 +402,7 @@ class Controller { */ protected function showMessage($view, $type, $message) { - return $this->load_partial($view, 'message', [ + return $this->loadPartial($view, 'message', [ 'message_type' => $type, 'message' => $message ]); @@ -416,7 +425,7 @@ class Controller { } $view->setStatusCode($code); - $this->render_full_page($view, $template, $data); + $this->renderFullPage($view, $template, $data); } /** diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index 5e3eb59e..7b04fa05 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -132,7 +132,7 @@ class Anime extends BaseController { AnimeWatchingStatus::COMPLETED => 'Completed' ]; - $this->set_session_redirect(); + $this->setSessionRedirect(); $this->outputHTML('anime/add', [ 'title' => $this->config->get('whose_list') . "'s Anime List · Add", @@ -166,7 +166,7 @@ class Anime extends BaseController { $this->set_flash_message('Failed to add new anime to list', 'error'); } - $this->session_redirect(); + $this->sessionRedirect(); } /** @@ -191,7 +191,7 @@ class Anime extends BaseController { ->titleize(); } - $this->set_session_redirect(); + $this->setSessionRedirect(); $this->outputHTML('anime/edit', [ 'title' => $this->config->get('whose_list') . @@ -240,7 +240,7 @@ class Anime extends BaseController { $this->set_flash_message('Failed to update anime.', 'error'); } - $this->session_redirect(); + $this->sessionRedirect(); } /** @@ -285,7 +285,7 @@ class Anime extends BaseController { $this->set_flash_message('Failed to delete anime.', 'error'); } - $this->session_redirect(); + $this->sessionRedirect(); } /** diff --git a/src/Controller/Collection.php b/src/Controller/Collection.php index e8955c5e..dc5b560b 100644 --- a/src/Controller/Collection.php +++ b/src/Controller/Collection.php @@ -115,7 +115,7 @@ class Collection extends BaseController { */ public function form($id = NULL) { - $this->set_session_redirect(); + $this->setSessionRedirect(); $action = (is_null($id)) ? "Add" : "Edit"; @@ -146,7 +146,7 @@ class Collection extends BaseController { $this->set_flash_message('Failed to update collection item', 'error'); } - $this->session_redirect(); + $this->sessionRedirect(); } /** @@ -167,7 +167,7 @@ class Collection extends BaseController { $this->set_flash_message('Failed to add collection item.', 'error'); } - $this->session_redirect(); + $this->sessionRedirect(); } /** diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index fc273abe..7bc3c9ec 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -115,7 +115,7 @@ class Manga extends Controller { ->titleize(); } - $this->set_session_redirect(); + $this->setSessionRedirect(); $this->outputHTML('manga/add', [ 'title' => $this->config->get('whose_list') . "'s Manga List · Add", @@ -149,7 +149,7 @@ class Manga extends Controller { $this->set_flash_message('Failed to add new manga to list' . $result['body'], 'error'); } - $this->session_redirect(); + $this->sessionRedirect(); } /** @@ -161,7 +161,7 @@ class Manga extends Controller { */ public function edit($id, $status = "All") { - $this->set_session_redirect(); + $this->setSessionRedirect(); $item = $this->model->getLibraryItem($id); $title = $this->config->get('whose_list') . "'s Manga List · Edit"; @@ -211,7 +211,7 @@ class Manga extends Controller { } - $this->session_redirect(); + $this->sessionRedirect(); } /** @@ -257,7 +257,7 @@ class Manga extends Controller { $this->set_flash_message('Failed to delete manga.', 'error'); } - $this->session_redirect(); + $this->sessionRedirect(); } /** diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 29f88fb9..97c2e656 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -52,9 +52,9 @@ class Dispatcher extends RoutingBase { /** * Routes added to router - * @var array $output_routes + * @var array $outputRoutes */ - protected $output_routes; + protected $outputRoutes; /** * Constructor @@ -68,7 +68,7 @@ class Dispatcher extends RoutingBase { $this->matcher = $container->get('aura-router')->getMatcher(); $this->request = $container->get('request'); - $this->output_routes = $this->_setupRoutes(); + $this->outputRoutes = $this->setupRoutes(); } /** @@ -80,12 +80,12 @@ class Dispatcher extends RoutingBase { { $logger = $this->container->getLogger('default'); - $raw_route = $this->request->getUri()->getPath(); - $route_path = "/" . trim($raw_route, '/'); + $rawRoute = $this->request->getUri()->getPath(); + $routePath = "/" . trim($rawRoute, '/'); $logger->info('Dispatcher - Routing data from get_route method'); $logger->info(print_r([ - 'route_path' => $route_path + 'route_path' => $routePath ], TRUE)); return $this->matcher->match($this->request); @@ -98,7 +98,7 @@ class Dispatcher extends RoutingBase { */ public function getOutputRoutes() { - return $this->output_routes; + return $this->outputRoutes; } /** @@ -131,10 +131,10 @@ class Dispatcher extends RoutingBase { { // If not route was matched, return an appropriate http // error message - $error_route = $this->getErrorParams(); + $errorRoute = $this->getErrorParams(); $controllerName = DEFAULT_CONTROLLER; - $actionMethod = $error_route['action_method']; - $params = $error_route['params']; + $actionMethod = $errorRoute['action_method']; + $params = $errorRoute['params']; } $this->call($controllerName, $actionMethod, $params); @@ -152,7 +152,7 @@ class Dispatcher extends RoutingBase { { if (array_key_exists('controller', $route->attributes)) { - $controller_name = $route->attributes['controller']; + $controllerName = $route->attributes['controller']; } else { @@ -160,13 +160,13 @@ class Dispatcher extends RoutingBase { } // Get the full namespace for a controller if a short name is given - if (strpos($controller_name, '\\') === FALSE) + if (strpos($controllerName, '\\') === FALSE) { $map = $this->getControllerList(); - $controller_name = $map[$controller_name]; + $controllerName = $map[$controllerName]; } - $action_method = (array_key_exists('action', $route->attributes)) + $actionMethod = (array_key_exists('action', $route->attributes)) ? $route->attributes['action'] : NOT_FOUND_METHOD; @@ -186,8 +186,8 @@ class Dispatcher extends RoutingBase { $logger->info(json_encode($params)); return [ - 'controller_name' => $controller_name, - 'action_method' => $action_method, + 'controller_name' => $controllerName, + 'action_method' => $actionMethod, 'params' => $params ]; } @@ -199,16 +199,16 @@ class Dispatcher extends RoutingBase { */ public function getController() { - $route_type = $this->__get('default_list'); - $request_uri = $this->request->getUri()->getPath(); - $path = trim($request_uri, '/'); + $routeType = $this->__get('default_list'); + $requestUri = $this->request->getUri()->getPath(); + $path = trim($requestUri, '/'); $segments = explode('/', $path); $controller = reset($segments); if (empty($controller)) { - $controller = $route_type; + $controller = $routeType; } return $controller; @@ -221,22 +221,22 @@ class Dispatcher extends RoutingBase { */ public function getControllerList() { - $default_namespace = DEFAULT_CONTROLLER_NAMESPACE; - $path = str_replace('\\', '/', $default_namespace); + $defaultNamespace = DEFAULT_CONTROLLER_NAMESPACE; + $path = str_replace('\\', '/', $defaultNamespace); $path = str_replace('Aviat/AnimeClient/', '', $path); $path = trim($path, '/'); - $actual_path = realpath(_dir(SRC_DIR, $path)); - $class_files = glob("{$actual_path}/*.php"); + $actualPath = realpath(_dir(SRC_DIR, $path)); + $classFiles = glob("{$actualPath}/*.php"); $controllers = []; - foreach ($class_files as $file) + foreach ($classFiles as $file) { - $raw_class_name = basename(str_replace(".php", "", $file)); - $path = strtolower(basename($raw_class_name)); - $class_name = trim($default_namespace . '\\' . $raw_class_name, '\\'); + $rawClassName = basename(str_replace(".php", "", $file)); + $path = strtolower(basename($rawClassName)); + $className = trim($defaultNamespace . '\\' . $rawClassName, '\\'); - $controllers[$path] = $class_name; + $controllers[$path] = $className; } return $controllers; @@ -260,6 +260,7 @@ class Dispatcher extends RoutingBase { // Run the appropriate controller method $logger->debug('Dispatcher - controller arguments'); $logger->debug(print_r($params, TRUE)); + call_user_func_array([$controller, $method], $params); } @@ -277,7 +278,7 @@ class Dispatcher extends RoutingBase { $logger->info('Dispatcher - failed route'); $logger->info(print_r($failure, TRUE)); - $action_method = ERROR_MESSAGE_METHOD; + $actionMethod = ERROR_MESSAGE_METHOD; $params = []; @@ -300,13 +301,13 @@ class Dispatcher extends RoutingBase { default: // Fall back to a 404 message - $action_method = NOT_FOUND_METHOD; + $actionMethod = NOT_FOUND_METHOD; break; } return [ 'params' => $params, - 'action_method' => $action_method + 'action_method' => $actionMethod ]; } @@ -315,9 +316,9 @@ class Dispatcher extends RoutingBase { * * @return array */ - protected function _setupRoutes() + protected function setupRoutes() { - $route_type = $this->getController(); + $routeType = $this->getController(); // Add routes $routes = []; @@ -326,18 +327,18 @@ class Dispatcher extends RoutingBase { $path = $route['path']; unset($route['path']); - $controller_map = $this->getControllerList(); - $controller_class = (array_key_exists($route_type, $controller_map)) - ? $controller_map[$route_type] + $controllerMap = $this->getControllerList(); + $controllerClass = (array_key_exists($routeType, $controllerMap)) + ? $controllerMap[$routeType] : DEFAULT_CONTROLLER; - if (array_key_exists($route_type, $controller_map)) + if (array_key_exists($routeType, $controllerMap)) { - $controller_class = $controller_map[$route_type]; + $controllerClass = $controllerMap[$routeType]; } // Prepend the controller to the route parameters - $route['controller'] = $controller_class; + $route['controller'] = $controllerClass; // Select the appropriate router method based on the http verb $add = (array_key_exists('verb', $route)) diff --git a/src/MenuGenerator.php b/src/MenuGenerator.php index fe09acf5..440d3f8c 100644 --- a/src/MenuGenerator.php +++ b/src/MenuGenerator.php @@ -62,7 +62,7 @@ class MenuGenerator extends UrlGenerator { * @param array $menus * @return array */ - protected function parse_config(array $menus) + protected function parseConfig(array $menus) { $parsed = []; @@ -88,17 +88,17 @@ class MenuGenerator extends UrlGenerator { public function generate($menu) { $menus = $this->config->get('menus'); - $parsed_config = $this->parse_config($menus); + $parsedConfig = $this->parseConfig($menus); // Bail out early on invalid menu - if ( ! $this->arr($parsed_config)->hasKey($menu)) + if ( ! $this->arr($parsedConfig)->hasKey($menu)) { return ''; } - $menu_config = $parsed_config[$menu]; + $menuConfig = $parsedConfig[$menu]; - foreach ($menu_config as $title => $path) + foreach ($menuConfig as $title => $path) { $has = $this->string($this->path())->contains($path); $selected = ($has && strlen($this->path()) >= strlen($path)); diff --git a/src/Util.php b/src/Util.php index 7cbcd427..e147a2e0 100644 --- a/src/Util.php +++ b/src/Util.php @@ -31,7 +31,7 @@ class Util { * Routes that don't require a second navigation level * @var array */ - private static $form_pages = [ + private static $formPages = [ 'edit', 'add', 'update', @@ -65,7 +65,7 @@ class Util { * @param string $b - Second item to compare * @return string */ - public static function is_selected($a, $b) + public static function isSelected($a, $b) { return ($a === $b) ? 'selected' : ''; } @@ -77,7 +77,7 @@ class Util { * @param string $b - Second item to compare * @return string */ - public static function is_not_selected($a, $b) + public static function isNotSelected($a, $b) { return ($a !== $b) ? 'selected' : ''; } @@ -87,13 +87,13 @@ class Util { * * @return bool */ - public function is_view_page() + public function isViewPage() { $url = $this->container->get('request') ->getUri(); - $page_segments = explode("/", (string) $url); + $pageSegments = explode("/", (string) $url); - $intersect = array_intersect($page_segments, self::$form_pages); + $intersect = array_intersect($pageSegments, self::$formPages); return empty($intersect); } @@ -104,9 +104,9 @@ class Util { * * @return boolean */ - public function is_form_page() + public function isFormPage() { - return ! $this->is_view_page(); + return ! $this->isViewPage(); } } diff --git a/tests/MenuGeneratorTest.php b/tests/MenuGeneratorTest.php index d412c9c9..dc63c6b0 100644 --- a/tests/MenuGeneratorTest.php +++ b/tests/MenuGeneratorTest.php @@ -62,7 +62,7 @@ class MenuGeneratorTest extends AnimeClientTestCase { 'All' => '/anime/all' ] ]; - $this->assertEquals($expected, $friend->parse_config($menus)); + $this->assertEquals($expected, $friend->parseConfig($menus)); } public function testBadConfig() diff --git a/tests/UtilTest.php b/tests/UtilTest.php index f67e9b53..5278c635 100644 --- a/tests/UtilTest.php +++ b/tests/UtilTest.php @@ -29,19 +29,19 @@ class UtilTest extends AnimeClientTestCase { public function testIsSelected() { // Failure to match - $this->assertEquals('', Util::is_selected('foo', 'bar')); + $this->assertEquals('', Util::isSelected('foo', 'bar')); // Matches - $this->assertEquals('selected', Util::is_selected('foo', 'foo')); + $this->assertEquals('selected', Util::isSelected('foo', 'foo')); } public function testIsNotSelected() { // Failure to match - $this->assertEquals('selected', Util::is_not_selected('foo', 'bar')); + $this->assertEquals('selected', Util::isNotSelected('foo', 'bar')); // Matches - $this->assertEquals('', Util::is_not_selected('foo', 'foo')); + $this->assertEquals('', Util::isNotSelected('foo', 'foo')); } public function dataIsViewPage() @@ -76,7 +76,7 @@ class UtilTest extends AnimeClientTestCase { 'REQUEST_URI' => $uri ] ]); - $this->assertEquals($expected, $this->util->is_view_page()); + $this->assertEquals($expected, $this->util->isViewPage()); } /** @@ -89,6 +89,6 @@ class UtilTest extends AnimeClientTestCase { 'REQUEST_URI' => $uri ] ]); - $this->assertEquals(!$expected, $this->util->is_form_page()); + $this->assertEquals(!$expected, $this->util->isFormPage()); } } diff --git a/tests/test_data/Kitsu/animeAfterTransform.json b/tests/test_data/Kitsu/animeAfterTransform.json index 19230fe7..32c29e95 100644 --- a/tests/test_data/Kitsu/animeAfterTransform.json +++ b/tests/test_data/Kitsu/animeAfterTransform.json @@ -15,7 +15,7 @@ "meta": { "name": "Crunchyroll", "link": true, - "logo": "<\/path><\/path><\/g><\/svg>" + "image": "streaming-logos\/crunchyroll.svg" }, "link": "http:\/\/www.crunchyroll.com\/attack-on-titan", "subs": ["en"], @@ -24,7 +24,7 @@ "meta": { "name": "Hulu", "link": true, - "logo": "<\/path><\/svg>" + "image": "streaming-logos\/hulu.svg" }, "link": "http:\/\/www.hulu.com\/attack-on-titan", "subs": ["en"], @@ -33,7 +33,7 @@ "meta": { "name": "Funimation", "link": true, - "logo": "<\/path><\/svg>" + "image": "streaming-logos\/funimation.svg" }, "link": "http:\/\/www.funimation.com\/shows\/attack-on-titan\/videos\/episodes", "subs": ["en"], @@ -42,7 +42,7 @@ "meta": { "name": "Netflix", "link": false, - "logo": "<\/path><\/svg>" + "image": "streaming-logos\/netflix.svg" }, "link": "t", "subs": ["en"],