Last active
August 29, 2015 14:05
-
-
Save johnkary/8bc4494d089b86ae7192 to your computer and use it in GitHub Desktop.
There's a delicate balance between useless instance variables and extracting well-named variables for readability.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ModuleController extends Controller | |
{ | |
public function getIconAction($moduleId) | |
{ | |
$moduleFinder = $this->get('module_finder'); | |
$module = $moduleFinder->getModule($moduleId); | |
if (!$module) { | |
return new Response('Could not find given moduleId', 404); | |
} | |
$logoPath = $module->getModuleFilePath('Resources/images/logo.png'); | |
// Return the module's logo, if one exists | |
if (file_exists($logoPath)) { | |
$image = file_get_contents($logoPath); | |
} | |
// Fallback to default logo | |
else { | |
$image = file_get_contents(__DIR__ . '/../Resources/images/dashlogo-small.png'); | |
} | |
return new Response($image, 200, ['Content-Type' => 'image/png']); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ModuleController extends Controller | |
{ | |
public function getIconAction($moduleId) | |
{ | |
$module = $this->get('module_finder')->getModule($moduleId); | |
if (!$module) { | |
return new Response('Could not find given Module for given moduleId', 404); | |
} | |
// Return the module's logo, if one exists | |
// else fallback default | |
$logoPath = $module->getModuleFilePath('Resources/images/logo.png'); | |
if (!file_exists($logoPath)) { | |
$logoPath = __DIR__ . '/../Resources/images/dashlogo-small.png'; | |
} | |
return new Response(file_get_contents($logoPath), 200, ['Content-Type' => 'image/png']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a good refactoring example!
When I'm writing methods, I like to start by declaring all the things I'll probably end up using. This gives me a nice block of variables at the top of the method that primes me for what to expect in the code that follows. It also has the benefit with phpstorm of letting me click on the variable names and see how they're used in the code below.
I tend to group the validation rules right next to the variable assignment, if possible, which is why you see the if (!$module) right under $module.
I totally agree with you about the redundant $image variable, that was a result of writing code at 5pm. :)
I like your refactored method much better, I just wanted to give you an overview of how I write/read code since I enjoyed seeing what your mental process was.