diff --git a/Extdn/Samples/Classes/ObjectManager.php b/Extdn/Samples/Classes/ObjectManager.php new file mode 100644 index 0000000..49c26dd --- /dev/null +++ b/Extdn/Samples/Classes/ObjectManager.php @@ -0,0 +1,15 @@ +getFilename()); + + $dependencyClasses = Reflection::getClassDependencies($className); + foreach ($dependencyClasses as $dependencyClass) { + if ($this->isObjectManagerAllowedWithClass($className)) { + continue; + } + + if (!$this->isInstanceOfObjectManager($dependencyClass->getName())) { + continue; + } + + $warning = 'The dependency "\\' . $dependencyClass->getName() . '" is not allowed here.'; + $phpcsFile->addWarning($warning, null, 'warning'); + } + } + + /** + * @param string $className + * + * @return bool + */ + private function isObjectManagerAllowedWithClass(string $className): bool + { + if (preg_match('/([a-zA-Z]+)(Factory|Builder)$/', $className)) { + return true; + } + + if (preg_match('/Proxy$/', $className)) { + return true; + } + + return false; + } + + /** + * @param string $className + * + * @return bool + */ + private function isInstanceOfObjectManager(string $className): bool + { + if (strstr($className, 'ObjectManager')) { + return true; + } + + return false; + } +} diff --git a/Extdn/Tests/Classes/ObjectManagerUnitTest.1.inc b/Extdn/Tests/Classes/ObjectManagerUnitTest.1.inc new file mode 100644 index 0000000..77fe63b --- /dev/null +++ b/Extdn/Tests/Classes/ObjectManagerUnitTest.1.inc @@ -0,0 +1,13 @@ + 1]; + } +} diff --git a/Extdn/ruleset.xml b/Extdn/ruleset.xml index 9490b4b..4ebc7ba 100644 --- a/Extdn/ruleset.xml +++ b/Extdn/ruleset.xml @@ -8,5 +8,6 @@ +