FactoryClass.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. Copyright (c) 2009 hamcrest.org
  4. */
  5. class FactoryClass
  6. {
  7. /**
  8. * @var string
  9. */
  10. private $file;
  11. /**
  12. * @var ReflectionClass
  13. */
  14. private $reflector;
  15. /**
  16. * @var array
  17. */
  18. private $methods;
  19. public function __construct($file, ReflectionClass $class)
  20. {
  21. $this->file = $file;
  22. $this->reflector = $class;
  23. $this->extractFactoryMethods();
  24. }
  25. public function extractFactoryMethods()
  26. {
  27. $this->methods = array();
  28. foreach ($this->getPublicStaticMethods() as $method) {
  29. if ($method->isFactory()) {
  30. // echo $this->getName() . '::' . $method->getName() . ' : ' . count($method->getCalls()) . PHP_EOL;
  31. $this->methods[] = $method;
  32. }
  33. }
  34. }
  35. public function getPublicStaticMethods()
  36. {
  37. $methods = array();
  38. foreach ($this->reflector->getMethods(ReflectionMethod::IS_STATIC) as $method) {
  39. if ($method->isPublic() && $method->getDeclaringClass() == $this->reflector) {
  40. $methods[] = new FactoryMethod($this, $method);
  41. }
  42. }
  43. return $methods;
  44. }
  45. public function getFile()
  46. {
  47. return $this->file;
  48. }
  49. public function getName()
  50. {
  51. return $this->reflector->name;
  52. }
  53. public function isFactory()
  54. {
  55. return !empty($this->methods);
  56. }
  57. public function getMethods()
  58. {
  59. return $this->methods;
  60. }
  61. }