Created
October 13, 2012 12:09
-
-
Save meza/3884365 to your computer and use it in GitHub Desktop.
Blog - Refactoring 1 - step1
This file contains 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 DemoClass | |
{ | |
public function someMethod() | |
{ | |
$val1 = SomeClass1::staticMethod1(); | |
$val2 = SomeClass2::staticMethod2(); | |
} | |
public function process() | |
{ | |
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fieldValue'])) { | |
return $this->_doSomethingWith($_POST['fieldValue']); | |
} | |
return null; | |
} | |
private function _doSomethingWith($field) | |
{ | |
return strrev((string)$field); | |
} | |
} | |
?> |
This file contains 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 | |
require "DemoCalss.php"; | |
$demo = new DemoClass(); | |
$demo->someMethod(); | |
print $demo->process(); | |
?> |
This file contains 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 | |
require "SomeClass1Wrapper.php"; | |
require "SomeClass2Wrapper.php"; | |
class DemoClassGlobals | |
{ | |
/** | |
* @var SomeClass1Wrapper | |
*/ | |
private $_someClass1; | |
/** | |
* @var SomeClass2Wrapper | |
*/ | |
private $_someClass2; | |
/** | |
* @var array | |
*/ | |
private $_serverVars = array(); | |
/** | |
* @var array | |
*/ | |
private $_postVars = array(); | |
public function __construct( | |
SomeClass1Wrapper $someClass1, | |
SomeClass2Wrapper $someClass2, | |
array $serverVars = array(), | |
array $postVars = array() | |
) | |
{ | |
$this->_someClass1 = $someClass1; | |
$this->_someClass2 = $someClass2; | |
$this->_serverVars = $serverVars; | |
$this->_postVars = $postVars; | |
} | |
public function someMethod() | |
{ | |
$val1 = $this->_someClass1->staticMethod1(); | |
$val2 = $this->_someClass2->staticMethod2(); | |
} | |
public function process() | |
{ | |
if ($this->_serverVars['REQUEST_METHOD'] === 'POST' && isset($this->_postVars['fieldValue'])) { | |
return $this->_doSomethingWith($this->_postVars['fieldValue']); | |
} | |
return null; | |
} | |
private function _doSomethingWith($field) | |
{ | |
return strrev((string)$field); | |
} | |
} | |
?> |
This file contains 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 | |
require "DemoClassGlobals.php"; | |
require "SomeClass1Wrapper.php"; | |
require "SomeClass2Wrapper.php"; | |
$someClass1 = new SomeClass1Wrapper(); | |
$someClass2 = new SomeClass2Wrapper(); | |
$demo = new DemoClassGlobals($someClass1, $someClass2, $_SERVER, $_POST); | |
$demo->someMethod(); | |
print $demo->process(); | |
?> |
This file contains 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 | |
require "SomeClass1Wrapper.php"; | |
require "SomeClass2Wrapper.php"; | |
class DemoClassTDA | |
{ | |
/** | |
* @var SomeClass1Wrapper | |
*/ | |
private $_someClass1; | |
/** | |
* @var SomeClass2Wrapper | |
*/ | |
private $_someClass2; | |
/** | |
* @var string | |
*/ | |
private $_requestMethod = 'GET'; | |
/** | |
* @var array | |
*/ | |
private $_postVars = array(); | |
public function __construct( | |
SomeClass1Wrapper $someClass1, | |
SomeClass2Wrapper $someClass2, | |
$requestMethod = 'GET', //assuming | |
array $postVars = array() | |
) | |
{ | |
$this->_someClass1 = $someClass1; | |
$this->_someClass2 = $someClass2; | |
$this->_requestMethod = $requestMethod; | |
$this->_postVars = $postVars; | |
} | |
public function someMethod() | |
{ | |
$val1 = $this->_someClass1->staticMethod1(); | |
$val2 = $this->_someClass2->staticMethod2(); | |
} | |
public function process() | |
{ | |
if ($this->_requestMethod === 'POST' && isset($this->_postVars['fieldValue'])) { | |
return $this->_doSomethingWith($this->_postVars['fieldValue']); | |
} | |
return null; | |
} | |
private function _doSomethingWith($field) | |
{ | |
return strrev((string)$field); | |
} | |
} | |
?> |
This file contains 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 | |
require "DemoClassTDA.php"; | |
require "SomeClass1Wrapper.php"; | |
require "SomeClass2Wrapper.php"; | |
$someClass1 = new SomeClass1Wrapper(); | |
$someClass2 = new SomeClass2Wrapper(); | |
$demo = new DemoClass($someClass1, $someClass2, $_SERVER['REQUEST_METHOD'], $_POST); | |
$demo->someMethod(); | |
print $demo->process(); | |
?> |
This file contains 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 | |
require_once 'PHPUnit/Framework.php'; | |
require_once dirname(__FILE__) . '/DemoClassTDA.php'; | |
class DemoClassTest extends PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @var DemoClass | |
*/ | |
protected $object; | |
/** | |
* @var someClass1Wrapper | |
*/ | |
protected $someClass1Mock; | |
/** | |
* @var someClass2Wrapper | |
*/ | |
protected $someClass2Mock; | |
/** | |
* Sets up the fixture, for example, opens a network connection. | |
* This method is called before a test is executed. | |
*/ | |
protected function setUp() | |
{ | |
$this->someClass1Mock = $this->getMock( | |
'someClass1Wrapper', | |
array('staticMethod1'), | |
array(), | |
'', | |
false); | |
$this->someClass2Mock = $this->getMock( | |
'someClass2Wrapper', | |
array('staticMethod2'), | |
array(), | |
'', | |
false); | |
} | |
/** | |
* Test that the static methods are called | |
*/ | |
public function testSomeMethod() | |
{ | |
$this->object = new DemoClass($this->someClass1Mock, $this->someClass2Mock); | |
$this->someClass1Mock->expects($this->at(0)) | |
->method('staticMethod1'); | |
$this->someClass2Mock->expects($this->at(0)) | |
->method('staticMethod2'); | |
$this->object->someMethod(); | |
} | |
/** | |
* Test that we get the expected null if the method is GET | |
*/ | |
public function testProcessWithGet() | |
{ | |
$this->object = new DemoClass($this->someClass1Mock, $this->someClass2Mock); | |
$expected = null; | |
$actual = $this->object->process(); | |
} | |
/** | |
* Test that we get the expected null if there's no 'fieldValue' | |
*/ | |
public function testProcessWithPostAndNoInput() | |
{ | |
$this->object = new DemoClass($this->someClass1Mock, $this->someClass2Mock, 'POST'); | |
$expected = null; | |
$actual = $this->object->process(); | |
} | |
/** | |
* Test that the process returns the reversed 'fieldValue' | |
*/ | |
public function testProcessWithPostAndInput() | |
{ | |
$input = 'Refactoring is fun'; | |
$postVars = array('fieldValue' => $input); | |
$this->object = new DemoClass( | |
$this->someClass1Mock, | |
$this->someClass2Mock, | |
'POST', | |
$postVars); | |
$expected = 'nuf si gnirotcafeR'; | |
$actual = $this->object->process(); | |
$this->assertEquals($expected, $actual); | |
} | |
} | |
?> |
This file contains 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 | |
require "SomeClass1Wrapper.php"; | |
require "SomeClass2Wrapper.php"; | |
class DemoClassWithWrapper | |
{ | |
public function someMethod( | |
SomeClass1Wrapper $someClass1, | |
SomeClass2Wrapper $someClass2 | |
) | |
{ | |
$val1 = $someClass1->staticMethod1(); | |
$val2 = $someClass2->staticMethod2(); | |
} | |
public function process() | |
{ | |
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fieldValue'])) { | |
return $this->_doSomethingWith($_POST['fieldValue']); | |
} | |
return null; | |
} | |
private function _doSomethingWith($field) | |
{ | |
return strrev((string)$field); | |
} | |
} | |
?> |
This file contains 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 | |
require "DemoClassWithWrapper.php"; | |
require "SomeClass1Wrapper.php"; | |
require "SomeClass2Wrapper.php"; | |
$demo = new DemoClassWithWrapper(); | |
$someClass1 = new SomeClass1Wrapper(); | |
$someClass2 = new SomeClass2Wrapper(); | |
$demo->someMethod($someClass1, $someClass2); | |
print $demo->process(); | |
?> |
This file contains 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
public function process() | |
{ | |
if ($this->_serverVars['REQUEST_METHOD'] === 'POST' && isset($this->_postVars['fieldValue'])) { | |
return $this->doSomethingWith($this->_postVars['fieldValue']); | |
} | |
... //some lines of other stuff | |
if ($this->_serverVars['REQUEST_METHOD'] === 'POST') { | |
$this->doSomethingElse(); | |
} | |
... //some lines of other stuff | |
if ($this->_serverVars['REQUEST_METHOD'] === 'POST') { | |
$this->doSomethingNaughty(true); | |
} | |
return null; | |
} |
This file contains 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 SomeClass1Wrapper | |
{ | |
public function staticMethod1() | |
{ | |
return SomeClass1::staticMethod1(); | |
} | |
} | |
?> |
This file contains 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 SomeClass2Wrapper | |
{ | |
public function staticMethod2() | |
{ | |
return SomeClass2::staticMethod2(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment