Blacklist.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /*
  3. * This file is part of the GlobalState package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\GlobalState;
  11. use ReflectionClass;
  12. /**
  13. * A blacklist for global state elements that should not be snapshotted.
  14. */
  15. class Blacklist
  16. {
  17. /**
  18. * @var array
  19. */
  20. private $globalVariables = array();
  21. /**
  22. * @var array
  23. */
  24. private $classes = array();
  25. /**
  26. * @var array
  27. */
  28. private $classNamePrefixes = array();
  29. /**
  30. * @var array
  31. */
  32. private $parentClasses = array();
  33. /**
  34. * @var array
  35. */
  36. private $interfaces = array();
  37. /**
  38. * @var array
  39. */
  40. private $staticAttributes = array();
  41. /**
  42. * @param string $variableName
  43. */
  44. public function addGlobalVariable($variableName)
  45. {
  46. $this->globalVariables[$variableName] = true;
  47. }
  48. /**
  49. * @param string $className
  50. */
  51. public function addClass($className)
  52. {
  53. $this->classes[] = $className;
  54. }
  55. /**
  56. * @param string $className
  57. */
  58. public function addSubclassesOf($className)
  59. {
  60. $this->parentClasses[] = $className;
  61. }
  62. /**
  63. * @param string $interfaceName
  64. */
  65. public function addImplementorsOf($interfaceName)
  66. {
  67. $this->interfaces[] = $interfaceName;
  68. }
  69. /**
  70. * @param string $classNamePrefix
  71. */
  72. public function addClassNamePrefix($classNamePrefix)
  73. {
  74. $this->classNamePrefixes[] = $classNamePrefix;
  75. }
  76. /**
  77. * @param string $className
  78. * @param string $attributeName
  79. */
  80. public function addStaticAttribute($className, $attributeName)
  81. {
  82. if (!isset($this->staticAttributes[$className])) {
  83. $this->staticAttributes[$className] = array();
  84. }
  85. $this->staticAttributes[$className][$attributeName] = true;
  86. }
  87. /**
  88. * @param string $variableName
  89. * @return bool
  90. */
  91. public function isGlobalVariableBlacklisted($variableName)
  92. {
  93. return isset($this->globalVariables[$variableName]);
  94. }
  95. /**
  96. * @param string $className
  97. * @param string $attributeName
  98. * @return bool
  99. */
  100. public function isStaticAttributeBlacklisted($className, $attributeName)
  101. {
  102. if (in_array($className, $this->classes)) {
  103. return true;
  104. }
  105. foreach ($this->classNamePrefixes as $prefix) {
  106. if (strpos($className, $prefix) === 0) {
  107. return true;
  108. }
  109. }
  110. $class = new ReflectionClass($className);
  111. foreach ($this->parentClasses as $type) {
  112. if ($class->isSubclassOf($type)) {
  113. return true;
  114. }
  115. }
  116. foreach ($this->interfaces as $type) {
  117. if ($class->implementsInterface($type)) {
  118. return true;
  119. }
  120. }
  121. if (isset($this->staticAttributes[$className][$attributeName])) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. }