Skip to content

Commit

Permalink
Refactor Inspector do not extend StaticObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaine Schmeisser committed Feb 1, 2013
1 parent 05abf7b commit f0dd98e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
36 changes: 31 additions & 5 deletions analysis/Inspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* can be used to gather information about any PHP source file for purposes of
* test metrics or static analysis.
*/
class Inspector extends \lithium\core\StaticObject {
class Inspector {

/**
* classes used
Expand Down Expand Up @@ -58,10 +58,7 @@ class Inspector extends \lithium\core\StaticObject {
*/
public static function isCallable($object, $method, $internal = false) {
$methodExists = method_exists($object, $method);
$callable = function($object, $method) {
return is_callable(array($object, $method));
};
return $internal ? $methodExists : $methodExists && $callable($object, $method);
return $internal ? $methodExists : $methodExists && is_callable(array($object, $method));
}

/**
Expand Down Expand Up @@ -585,6 +582,35 @@ protected static function _modifiers($inspector, $list = array()) {
return (method_exists($inspector, $method) && $inspector->{$method}());
});
}

/**
* Returns an instance of a class with given `config`. The `name` could be a key from the
* `classes` array, a fully namespaced class name, or an object. Typically this method is used
* in `_init` to create the dependencies used in the current class.
*
* @param string|object $name A `classes` key or fully-namespaced class name.
* @param array $options The configuration passed to the constructor.
* @return object
*/
protected static function _instance($name, array $options = array()) {
if (is_string($name) && isset(static::$_classes[$name])) {
$name = static::$_classes[$name];
}
return Libraries::instance(null, $name, $options);
}

/**
* Calls a method on this object with the given parameters. Provides an OO wrapper for
* `forward_static_call_array()`.
*
* @param string $method Name of the method to call.
* @param array $params Parameter list to use when calling `$method`.
* @return mixed Returns the result of the method call.
*/
public static function invokeMethod($method, $params = array()) {
return forward_static_call_array(array(get_called_class(), $method), $params);
}

}

?>
39 changes: 38 additions & 1 deletion tests/cases/analysis/InspectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class InspectorTest extends \lithium\test\Unit {
* @return void
*/
public function testBasicMethodInspection() {
$class = 'lithium\analysis\Inspector';
$class = 'lithium\analysis\Debugger';
$parent = 'lithium\core\StaticObject';

$expected = array_diff(get_class_methods($class), get_class_methods($parent));
Expand Down Expand Up @@ -350,6 +350,43 @@ public function testCallableVisibility() {
$this->assertTrue(Inspector::isCallable('lithium\action\Dispatcher', '_callable', 1));
}

/**
* Tests that the correct parameters are always passed in `Inspector::invokeMethod()`,
* regardless of the number.
*
* @return void
*/
public function testMethodInvocationWithParameters() {
$class = 'lithium\tests\mocks\analysis\MockInspector';

$this->assertEqual($class::invokeMethod('foo'), array());
$this->assertEqual($class::invokeMethod('foo', array('bar')), array('bar'));

$params = array('one', 'two');
$this->assertEqual($class::invokeMethod('foo', $params), $params);

$params = array('short', 'parameter', 'list');
$this->assertEqual($class::invokeMethod('foo', $params), $params);

$params = array('a', 'longer', 'parameter', 'list');
$this->assertEqual($class::invokeMethod('foo', $params), $params);

$params = array('a', 'much', 'longer', 'parameter', 'list');
$this->assertEqual($class::invokeMethod('foo', $params), $params);

$params = array('an', 'extremely', 'long', 'list', 'of', 'parameters');
$this->assertEqual($class::invokeMethod('foo', $params), $params);

$params = array('an', 'extremely', 'long', 'list', 'of', 'parameters');
$this->assertEqual($class::invokeMethod('foo', $params), $params);

$params = array(
'if', 'you', 'have', 'a', 'parameter', 'list', 'this',
'long', 'then', 'UR', 'DOIN', 'IT', 'RONG'
);
$this->assertEqual($class::invokeMethod('foo', $params), $params);
}

}

?>
8 changes: 4 additions & 4 deletions tests/cases/test/MockerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ public function testFilteringStaticClass() {
}

public function testFilteringStaticClassCanReturnOriginal() {
$mockee = 'lithium\analysis\inspector\Mock';
$mockee = 'lithium\analysis\debugger\Mock';

$originalResult = $mockee::methods('lithium\analysis\Inspector');
$originalResult = $mockee::export(array('foo', 'bar', 'baz'));

$mockee::applyFilter('tokenize', function($self, $params, $chain) {
$mockee::applyFilter('export', function($self, $params, $chain) {
return $chain->next($self, $params, $chain);
});

$filteredResult = $mockee::methods('lithium\analysis\Inspector');
$filteredResult = $mockee::export(array('foo', 'bar', 'baz'));

$this->assertEqual($filteredResult, $originalResult);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/mocks/analysis/MockInspector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\tests\mocks\analysis;

class MockInspector extends \lithium\analysis\Inspector {

public static function foo() {
$args = func_get_args();
return $args;
}

}

?>

0 comments on commit f0dd98e

Please sign in to comment.