![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mcoil.corals.io/vendor/kkszymanowski/traitor/src/ |
<?php /* * KKSzymanowski/Traitor * Add a trait use statement to existing class * * @package KKSzymanowski/Traitor * @author Kuba Szymanowski <[email protected]> * @link https://github.com/kkszymanowski/traitor * @license MIT */ namespace Traitor; use BadMethodCallException; use ReflectionClass; use RuntimeException; use Traitor\Handlers\AbstractTreeHandler; class TraitUseAdder { /** @var array */ protected $traitReflections = []; /** * @param string $trait * @return static */ public function addTrait($trait) { return $this->addTraits([$trait]); } /** * @param array $traits * @return static */ public function addTraits(array $traits) { foreach ($traits as $trait) { $this->traitReflections[] = new ReflectionClass($trait); } return $this; } /** * @param string $class * @return $this * * @throws BadMethodCallException * @throws RuntimeException */ public function toClass($class) { if (count($this->traitReflections) == 0) { throw new BadMethodCallException("No traits to add were found. Call 'addTrait' first."); } $classReflection = new ReflectionClass($class); $filePath = $classReflection->getFileName(); $content = file($filePath); foreach ($this->traitReflections as $traitReflection) { $handler = new AbstractTreeHandler( $content, $traitReflection->getName(), $classReflection->getName() ); $content = $handler->handle()->toArray(); } file_put_contents($filePath, implode($content)); return $this; } }