/** * Resolve AbstractFactory * * @param ResolvedClass $resolved */ public function resolve(ResolvedClass $resolved) { $class = $resolved->getClass(); $pattern = new AbstractFactory($class->getFullname()); $resolver = new TypeResolver(); // we don't look private and non static methods $collection = (new Collection($class->getMethods()))->where(sprintf('method => method.getVisibility() == "public" and method.getState() == %s', ReflectedMethod::STATE_STATIC)); if (empty($collection)) { return; } /** @var ReflectedMethod $method */ foreach ($collection as $method) { // method instanciates at least one object $instanciated = array_unique($method->getInstanciedClasses()); if (empty($instanciated)) { continue; } foreach ($method->getReturns() as $return) { // returns an object if ($resolver->isNative($return->getType())) { continue; } // doesn't return itself (avoid singleton) if (in_array($return->getType(), array('\\self', '\\static', $class->getFullname()))) { continue; } $pattern->setCreated($return->getType()); $resolved->pushPattern($pattern); return; } } }
/** * @return array */ public function getDependencies() { // on read : compare with aliases. We cannot make it in pushDependency() => aliases aren't yet known $dependencies = array(); foreach ($this->dependencies as $name) { array_push($dependencies, $this->nameResolver->resolve($name, null)); } // returned values $resolver = new TypeResolver(); foreach ($this->returns as $return) { $name = $return->getType(); if (!$resolver->isNative($name)) { array_push($dependencies, $this->nameResolver->resolve($name, null)); } } // anonymous classes in method (inner class) foreach ($this->anonymousClasses as $c) { array_push($dependencies, $c->getParent()); $dependencies = array_merge($dependencies, $c->getDependencies()); } return array_unique($dependencies); }
/** * @dataProvider providesTypes */ public function testTypeResolvesWorks($string, $expected) { $resolver = new TypeResolver(); $this->assertEquals($expected, $resolver->resolve($string)); }
/** * Extract the list of returned values * * @param ReflectedMethod $method * @return $this */ private function extractReturns(ReflectedMethod $method, $n, TokenCollection $tokens) { $resolver = new TypeResolver(); // PHP 7 // we cannot use specific token. The ":" delimiter is a T_STRING token $following = $this->searcher->getUnder(array('{', ';'), $n, $tokens); if (preg_match('!:(.*)!', $following, $matches)) { $type = trim($matches[1]); if (empty($type)) { return $this; } $return = new ReflectedReturn($type, ReflectedReturn::VALUE_UNKNOW, ReflectedReturn::STRICT_TYPE_HINT); $method->pushReturn($return); return $this; } // array of available values based on code if (preg_match_all('!([\\s;]return\\s|^return\\s+)(.*?);!', $method->getContent(), $matches)) { foreach ($matches[2] as $m) { $value = trim($m, ";\t\n\r\v"); $return = new ReflectedReturn($resolver->resolve($m), $value, ReflectedReturn::ESTIMATED_TYPE_HINT); $method->pushReturn($return); } } return $this; }