-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from iannsp/master
extract some methods
- Loading branch information
Showing
7 changed files
with
311 additions
and
125 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
namespace Respect; | ||
|
||
use \SplObjectStorage; | ||
use \ReflectionClass; | ||
use \ReflectionMethod; | ||
use \ReflectionProperty; | ||
|
||
|
||
/** | ||
* DocItem Reflection Class to speak where is the socks to rock it. | ||
* | ||
* @author Ivo Nascimento <[email protected]> | ||
*/ | ||
class DocItem | ||
{ | ||
private $item; | ||
private $refItem; | ||
|
||
/** | ||
* __construct Construct a doc Item | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function __construct($item) | ||
{ | ||
$this->docItem = $item; | ||
$this->refItem = new ReflectionClass($item); | ||
} | ||
private function getName() | ||
{ | ||
return $this->refItem->getName(); | ||
} | ||
private function getDocComment() | ||
{ | ||
return $this->refItem->getDocComment(); | ||
} | ||
public function __call($method, $parameters) | ||
{ | ||
return call_user_func_array(array($this->refItem, $method),$parameters ); | ||
} | ||
private function getMethods($scope=null) | ||
{ | ||
if (is_null($scope)) | ||
return $this->refItem->getMethods(); | ||
return $this->refItem->getMethods($scope); | ||
} | ||
public function getSections() | ||
{ | ||
$testCaseClass = $this->getName().'Test'; | ||
|
||
if (class_exists($testCaseClass)) { | ||
$testCaseReflection = new ReflectionClass($testCaseClass); | ||
$testCaseMethods = $testCaseReflection->getMethods(); | ||
} else { | ||
$testCaseMethods= array(); | ||
} | ||
$sections = new SplObjectStorage; | ||
$methods = $this->getMethods(ReflectionMethod::IS_PUBLIC ^ ReflectionMethod::IS_STATIC); | ||
$properties = $this->getProperties(ReflectionProperty::IS_PUBLIC ^ ReflectionProperty::IS_STATIC); | ||
$staticMethods = $this->getMethods(ReflectionMethod::IS_PUBLIC & ReflectionMethod::IS_STATIC); | ||
$staticProperties = $this->getProperties(ReflectionProperty::IS_PUBLIC & ReflectionProperty::IS_STATIC); | ||
|
||
$nameSort = function($a, $b) { | ||
return strnatcasecmp($a->getName(), $b->getName()); | ||
}; | ||
|
||
usort($methods, $nameSort); | ||
usort($properties, $nameSort); | ||
usort($staticMethods, $nameSort); | ||
usort($staticProperties, $nameSort); | ||
foreach (array_merge($staticProperties, $properties, $staticMethods, $methods) as $method) | ||
$sections[$method] = array_values(array_filter($testCaseMethods, function($test) use($method) { | ||
return 0 === stripos($test->getName(), 'test_as_example_for_'.$method->getName().'_method'); | ||
})); | ||
return $sections; | ||
} | ||
public function getClassContent() | ||
{ | ||
$sections = array(); | ||
$class = $this->getName(); | ||
$sections[$class] = $this->getDocComment(); | ||
$reflectors = $this->getSections(); | ||
foreach ($reflectors as $sub) { | ||
$tests = $reflectors[$sub]; | ||
if ($sub->isStatic()) | ||
$subName = 'static '; | ||
else | ||
$subName = ''; | ||
|
||
$subName .= $sub->getName(); | ||
$name = $class.'::'.$subName; | ||
|
||
if ($sub instanceof ReflectionMethod) | ||
if ($sub->getNumberOfRequiredParameters() <= 0) | ||
$name .= '()'; | ||
else { | ||
$params = $sub->getParameters(); | ||
$tmp = array(); | ||
foreach ($params as $param) { | ||
if ($param->isArray()) | ||
$tmp[] = 'array '; | ||
if ($param->isOptional()) | ||
$tmp[] = '$'.$param->getName().'=null'; | ||
elseif ($param->isDefaultValueAvailable()) | ||
$tmp[] = '$'.$param->getName().'='.$param->getDefaultValue(); | ||
else | ||
$tmp[] = '$'.$param->getName(); | ||
} | ||
$name .= '('.implode(', ', $tmp).')'; | ||
} | ||
|
||
$sections[$name] = $sub->getDocComment(); | ||
// Fetch method content for examples | ||
foreach ($tests as $n => $test) { | ||
$testCaseContents = file($test->getFilename()); | ||
$testSectionName = "Example ".($n+1).":"; | ||
$testCaseLines = array_slice($testCaseContents, 1+$test->getStartLine(), -2+$test->getEndLine()-$test->getStartLine()); | ||
$testCaseLines = array_map( | ||
function($line) { | ||
if ($line{0} == "\t") | ||
return substr($line, 1); | ||
if ($line{0} == ' ') | ||
return substr($line, 4); | ||
else | ||
return ' ' . $line; | ||
}, | ||
$testCaseLines | ||
); | ||
$sections[$name] .= PHP_EOL.PHP_EOL.$testSectionName.PHP_EOL.PHP_EOL.implode($testCaseLines); | ||
} | ||
|
||
} | ||
return $sections; | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Respect; | ||
|
||
/** | ||
* speak Markdownish | ||
* | ||
* @author Ivo Nascimento <[email protected]> | ||
*/ | ||
class MarkDown | ||
{ | ||
public function get(array $sections) | ||
{ | ||
$string = array(); | ||
foreach ($sections as $name=>$content) { | ||
|
||
if (preg_match_all('/[\:]{1,2}(.*)/', $name, $matches)) | ||
$name = $matches[1][0]; | ||
else | ||
$matches = 1; | ||
|
||
$content = trim(preg_replace('#^(\s*[*]|[/][*]{2}|[\][*])[/*]*(.*?)[ /*]*$#m', '$2', $content)); | ||
$content = preg_replace("#\\n\\n[ ]*@#", "\n\nMore Info:\n\n@", $content); | ||
$content = preg_replace_callback('#^[ ]*[@](\w+)[ ]+(.*)#mi', | ||
function($matches){ | ||
$matches[1] = ucfirst($matches[1]); | ||
return " - **{$matches[1]}:** {$matches[2]} "; | ||
}, | ||
$content); | ||
$char = count($matches) == 1 ? '=' : '-'; | ||
$string[] = trim($name . "\n" . str_repeat($char, strlen($name)) . "\n\n" . $content); | ||
} | ||
return implode("\n\n", $string); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
namespace respect; | ||
|
||
class DocItemTest extends \phpunit_framework_testcase | ||
{ | ||
private $docItem; | ||
public function setUp() | ||
{ | ||
$this->docItem = New DocItem( 'Respect\DocItem'); | ||
} | ||
public function tearDown() | ||
{ | ||
$this->docItem = null; | ||
} | ||
public function test_getName() | ||
{ | ||
$this->AssertEquals('Respect\DocItem', $this->docItem->getName()); | ||
} | ||
public function test_getDocComment() | ||
{ | ||
$returnValue = "/** | ||
* DocItem Reflection Class to speak where is the socks to rock it. | ||
* | ||
* @author Ivo Nascimento <[email protected]> | ||
*/"; | ||
$this->AssertEquals($returnValue, $this->docItem->getDocComment()); | ||
} | ||
|
||
public function getMethodsDataprovider() | ||
{ | ||
return array( | ||
array(0,'__construct'), | ||
array(1,'getName'), | ||
array(2,'getDocComment'), | ||
array(3,'__call') | ||
); | ||
} | ||
/** | ||
* @dataProvider getMethodsDataprovider | ||
*/ | ||
public function test_getMethods($idx, $name) | ||
{ | ||
$methods = $this->docItem->getMethods(); | ||
$this->AssertEquals($methods[$idx]->name, $name ); | ||
} | ||
public function test_getSections() | ||
{ | ||
$this->AssertInstanceOf('SplObjectStorage',$this->docItem->getSections()); | ||
} | ||
public function test_getClassContents() | ||
{ | ||
$doc = new Doc('\Respect\Doc'); | ||
$content = \file_get_contents (__DIR__."/../../output/RespectDoc_output.txt"); | ||
$this->AssertEquals($content, (string)$doc); | ||
} | ||
} |
Oops, something went wrong.