Blacklist.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of sebastian/global-state.
  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. declare(strict_types=1);
  11. namespace SebastianBergmann\GlobalState;
  12. use ReflectionClass;
  13. /**
  14. * A blacklist for global state elements that should not be snapshotted.
  15. */
  16. class Blacklist
  17. {
  18. /**
  19. * @var array
  20. */
  21. private $globalVariables = [];
  22. /**
  23. * @var string[]
  24. */
  25. private $classes = [];
  26. /**
  27. * @var string[]
  28. */
  29. private $classNamePrefixes = [];
  30. /**
  31. * @var string[]
  32. */
  33. private $parentClasses = [];
  34. /**
  35. * @var string[]
  36. */
  37. private $interfaces = [];
  38. /**
  39. * @var array
  40. */
  41. private $staticAttributes = [];
  42. public function addGlobalVariable(string $variableName)
  43. {
  44. $this->globalVariables[$variableName] = true;
  45. }
  46. public function addClass(string $className)
  47. {
  48. $this->classes[] = $className;
  49. }
  50. public function addSubclassesOf(string $className)
  51. {
  52. $this->parentClasses[] = $className;
  53. }
  54. public function addImplementorsOf(string $interfaceName)
  55. {
  56. $this->interfaces[] = $interfaceName;
  57. }
  58. public function addClassNamePrefix(string $classNamePrefix)
  59. {
  60. $this->classNamePrefixes[] = $classNamePrefix;
  61. }
  62. public function addStaticAttribute(string $className, string $attributeName)
  63. {
  64. if (!isset($this->staticAttributes[$className])) {
  65. $this->staticAttributes[$className] = [];
  66. }
  67. $this->staticAttributes[$className][$attributeName] = true;
  68. }
  69. public function isGlobalVariableBlacklisted(string $variableName): bool
  70. {
  71. return isset($this->globalVariables[$variableName]);
  72. }
  73. public function isStaticAttributeBlacklisted(string $className, string $attributeName): bool
  74. {
  75. if (\in_array($className, $this->classes)) {
  76. return true;
  77. }
  78. foreach ($this->classNamePrefixes as $prefix) {
  79. if (\strpos($className, $prefix) === 0) {
  80. return true;
  81. }
  82. }
  83. $class = new ReflectionClass($className);
  84. foreach ($this->parentClasses as $type) {
  85. if ($class->isSubclassOf($type)) {
  86. return true;
  87. }
  88. }
  89. foreach ($this->interfaces as $type) {
  90. if ($class->implementsInterface($type)) {
  91. return true;
  92. }
  93. }
  94. if (isset($this->staticAttributes[$className][$attributeName])) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. }