Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Refactor `Inspector` do not extend `StaticObject`. #810

Merged
merged 1 commit into from

3 participants

@blainesch
Collaborator

No description provided.

@jails
Collaborator

Ok my bad, I only take the "filtering feature" into consideration but looks like Inspector extending StaticObject is consistent here.

@nateabele nateabele merged commit 88bc10e into UnionOfRAD:dev

1 check passed

Details default The Travis build passed
@blainesch blainesch deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 1, 2013
  1. Refactor `Inspector` do not extend `StaticObject`.

    Blaine Schmeisser authored
This page is out of date. Refresh to see the latest.
View
36 analysis/Inspector.php
@@ -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
@@ -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));
}
/**
@@ -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);
+ }
+
}
?>
View
39 tests/cases/analysis/InspectorTest.php
@@ -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));
@@ -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);
+ }
+
}
?>
View
8 tests/cases/test/MockerTest.php
@@ -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);
}
View
20 tests/mocks/analysis/MockInspector.php
@@ -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;
+ }
+
+}
+
+?>
Something went wrong with that request. Please try again.