4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Doubler\ClassPatch;
14 use Prophecy\Doubler\Generator\Node\ClassNode;
15 use Prophecy\Doubler\Generator\Node\MethodNode;
19 * Makes SplFileInfo and derivative classes usable with Prophecy.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 class SplFileInfoPatch implements ClassPatchInterface
26 * Supports everything that extends SplFileInfo.
28 * @param ClassNode $node
32 public function supports(ClassNode $node)
34 if (null === $node->getParentClass()) {
37 return 'SplFileInfo' === $node->getParentClass()
38 || is_subclass_of($node->getParentClass(), 'SplFileInfo')
43 * Updated constructor code to call parent one with dummy file argument.
45 * @param ClassNode $node
47 public function apply(ClassNode $node)
49 if ($node->hasMethod('__construct')) {
50 $constructor = $node->getMethod('__construct');
52 $constructor = new MethodNode('__construct');
53 $node->addMethod($constructor);
56 if ($this->nodeIsDirectoryIterator($node)) {
57 $constructor->setCode('return parent::__construct("' . __DIR__ . '");');
62 if ($this->nodeIsSplFileObject($node)) {
63 $filePath = str_replace('\\','\\\\',__FILE__);
64 $constructor->setCode('return parent::__construct("' . $filePath .'");');
69 if ($this->nodeIsSymfonySplFileInfo($node)) {
70 $filePath = str_replace('\\','\\\\',__FILE__);
71 $constructor->setCode('return parent::__construct("' . $filePath .'", "", "");');
76 $constructor->useParentCode();
80 * Returns patch priority, which determines when patch will be applied.
82 * @return int Priority number (higher - earlier)
84 public function getPriority()
90 * @param ClassNode $node
93 private function nodeIsDirectoryIterator(ClassNode $node)
95 $parent = $node->getParentClass();
97 return 'DirectoryIterator' === $parent
98 || is_subclass_of($parent, 'DirectoryIterator');
102 * @param ClassNode $node
105 private function nodeIsSplFileObject(ClassNode $node)
107 $parent = $node->getParentClass();
109 return 'SplFileObject' === $parent
110 || is_subclass_of($parent, 'SplFileObject');
114 * @param ClassNode $node
117 private function nodeIsSymfonySplFileInfo(ClassNode $node)
119 $parent = $node->getParentClass();
121 return 'Symfony\\Component\\Finder\\SplFileInfo' === $parent;