inspect() public static method

Gets the ReflectionClass instance of a class.
public static inspect ( string $class ) : object
$class string The class name to inspect.
return object The ReflectionClass instance.
Beispiel #1
0
 /**
  * The Constructor.
  *
  * @param mixed $reference An instance or a fully-namespaced class name.
  */
 public function __construct($reference)
 {
     $reference = $this->_reference($reference);
     $isString = is_string($reference);
     if ($isString) {
         if (!class_exists($reference)) {
             throw new InvalidArgumentException("Can't Stub the unexisting class `{$reference}`.");
         }
         $reference = ltrim($reference, '\\');
         $reflection = Inspector::inspect($reference);
     } else {
         $reflection = Inspector::inspect(get_class($reference));
     }
     if (!$reflection->isInternal()) {
         $this->_reference = $reference;
         return;
     }
     if (!$isString) {
         throw new InvalidArgumentException("Can't Stub built-in PHP instances, create a test double using `Double::instance()`.");
     }
     $this->_needToBePatched = true;
     return $this->_reference = $reference;
 }
Beispiel #2
0
 /**
  * Gets the parameters array of a class method.
  *
  * @param  $class  The class name.
  * @param  $method The method name.
  * @param  $data   The default values.
  * @return array   The parameters array.
  */
 public static function parameters($class, $method, $data = null)
 {
     $params = [];
     $reflexion = Inspector::inspect($class);
     $parameters = $reflexion->getMethod($method)->getParameters();
     if ($data === null) {
         return $parameters;
     }
     foreach ($data as $key => $value) {
         $name = $key;
         if ($parameters) {
             $parameter = array_shift($parameters);
             $name = $parameter->getName();
         }
         $params[$name] = $value;
     }
     foreach ($parameters as $parameter) {
         if ($parameter->isDefaultValueAvailable()) {
             $params[$parameter->getName()] = $parameter->getDefaultValue();
         }
     }
     return $params;
 }
Beispiel #3
0
<?php

namespace kahlan\spec\suite\analysis;

use kahlan\analysis\Inspector;
describe("Inspector", function () {
    before(function () {
        $this->class = 'kahlan\\spec\\fixture\\analysis\\SampleClass';
    });
    describe('::inspect()', function () {
        it("gets the reflexion layer of a class", function () {
            $inspector = Inspector::inspect($this->class);
            expect($inspector)->toBeAnInstanceOf('ReflectionClass');
            expect($inspector->name)->toBe($this->class);
        });
    });
    describe('::parameters()', function () {
        it("gets method's parameters details", function () {
            $inspector = Inspector::parameters($this->class, 'parametersExample');
            expect($inspector)->toBeA('array');
            $param2 = next($inspector);
            expect($param2)->toBeAnInstanceOf('ReflectionParameter');
            expect($param2->getName())->toBe('b');
            expect($param2->getDefaultValue())->toBe(100);
            $param3 = next($inspector);
            expect($param3)->toBeAnInstanceOf('ReflectionParameter');
            expect($param3->getName())->toBe('c');
            expect($param3->getDefaultValue())->toBe('abc');
            $param4 = next($inspector);
            expect($param4)->toBeAnInstanceOf('ReflectionParameter');
            expect($param4->getName())->toBe('d');
Beispiel #4
0
 /**
  * Creates method definitions from an interface array.
  *
  * @param  array   $interfaces A array on interfaces.
  * @param  integer $mask       The method mask to filter.
  * @return string              The generated methods.
  */
 protected static function _generateInterfaceMethods($interfaces, $mask = 255)
 {
     if (!$interfaces) {
         return [];
     }
     $result = [];
     foreach ((array) $interfaces as $interface) {
         if (!interface_exists($interface)) {
             throw new MissingImplementationException("Unexisting interface `{$interface}`");
         }
         $reflection = Inspector::inspect($interface);
         $methods = $reflection->getMethods($mask);
         foreach ($methods as $method) {
             $result[$method->getName()] = static::_generateMethod($method);
         }
     }
     return $result;
 }
Beispiel #5
0
 /**
  * Check the actual value can receive messages.
  *
  * @param mixed $reference An instance or a fully-namespaced class name.
  */
 protected function _check($reference)
 {
     $isString = is_string($reference);
     if ($isString) {
         if (!class_exists($reference)) {
             throw new InvalidArgumentException("Can't Spy the unexisting class `{$reference}`.");
         }
         $reflection = Inspector::inspect($reference);
     } else {
         $reflection = Inspector::inspect(get_class($reference));
     }
     if (!$reflection->isInternal()) {
         return;
     }
     if (!$isString) {
         throw new InvalidArgumentException("Can't Spy built-in PHP instances, create a test double using `Double::instance()`.");
     }
 }