FactoryClass.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }