annotate vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/SplFileInfoPatch.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 7a779792577d
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Prophecy.
Chris@0 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 6 * Marcello Duarte <marcello.duarte@gmail.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Prophecy\Doubler\ClassPatch;
Chris@0 13
Chris@0 14 use Prophecy\Doubler\Generator\Node\ClassNode;
Chris@0 15 use Prophecy\Doubler\Generator\Node\MethodNode;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * SplFileInfo patch.
Chris@0 19 * Makes SplFileInfo and derivative classes usable with Prophecy.
Chris@0 20 *
Chris@0 21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 22 */
Chris@0 23 class SplFileInfoPatch implements ClassPatchInterface
Chris@0 24 {
Chris@0 25 /**
Chris@0 26 * Supports everything that extends SplFileInfo.
Chris@0 27 *
Chris@0 28 * @param ClassNode $node
Chris@0 29 *
Chris@0 30 * @return bool
Chris@0 31 */
Chris@0 32 public function supports(ClassNode $node)
Chris@0 33 {
Chris@0 34 if (null === $node->getParentClass()) {
Chris@0 35 return false;
Chris@0 36 }
Chris@0 37 return 'SplFileInfo' === $node->getParentClass()
Chris@0 38 || is_subclass_of($node->getParentClass(), 'SplFileInfo')
Chris@0 39 ;
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Updated constructor code to call parent one with dummy file argument.
Chris@0 44 *
Chris@0 45 * @param ClassNode $node
Chris@0 46 */
Chris@0 47 public function apply(ClassNode $node)
Chris@0 48 {
Chris@0 49 if ($node->hasMethod('__construct')) {
Chris@0 50 $constructor = $node->getMethod('__construct');
Chris@0 51 } else {
Chris@0 52 $constructor = new MethodNode('__construct');
Chris@0 53 $node->addMethod($constructor);
Chris@0 54 }
Chris@0 55
Chris@0 56 if ($this->nodeIsDirectoryIterator($node)) {
Chris@0 57 $constructor->setCode('return parent::__construct("' . __DIR__ . '");');
Chris@0 58
Chris@0 59 return;
Chris@0 60 }
Chris@0 61
Chris@0 62 if ($this->nodeIsSplFileObject($node)) {
Chris@12 63 $filePath = str_replace('\\','\\\\',__FILE__);
Chris@12 64 $constructor->setCode('return parent::__construct("' . $filePath .'");');
Chris@12 65
Chris@12 66 return;
Chris@12 67 }
Chris@12 68
Chris@12 69 if ($this->nodeIsSymfonySplFileInfo($node)) {
Chris@12 70 $filePath = str_replace('\\','\\\\',__FILE__);
Chris@12 71 $constructor->setCode('return parent::__construct("' . $filePath .'", "", "");');
Chris@0 72
Chris@0 73 return;
Chris@0 74 }
Chris@0 75
Chris@0 76 $constructor->useParentCode();
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Returns patch priority, which determines when patch will be applied.
Chris@0 81 *
Chris@0 82 * @return int Priority number (higher - earlier)
Chris@0 83 */
Chris@0 84 public function getPriority()
Chris@0 85 {
Chris@0 86 return 50;
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * @param ClassNode $node
Chris@0 91 * @return boolean
Chris@0 92 */
Chris@0 93 private function nodeIsDirectoryIterator(ClassNode $node)
Chris@0 94 {
Chris@0 95 $parent = $node->getParentClass();
Chris@0 96
Chris@0 97 return 'DirectoryIterator' === $parent
Chris@0 98 || is_subclass_of($parent, 'DirectoryIterator');
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * @param ClassNode $node
Chris@0 103 * @return boolean
Chris@0 104 */
Chris@0 105 private function nodeIsSplFileObject(ClassNode $node)
Chris@0 106 {
Chris@0 107 $parent = $node->getParentClass();
Chris@0 108
Chris@0 109 return 'SplFileObject' === $parent
Chris@0 110 || is_subclass_of($parent, 'SplFileObject');
Chris@0 111 }
Chris@12 112
Chris@12 113 /**
Chris@12 114 * @param ClassNode $node
Chris@12 115 * @return boolean
Chris@12 116 */
Chris@12 117 private function nodeIsSymfonySplFileInfo(ClassNode $node)
Chris@12 118 {
Chris@12 119 $parent = $node->getParentClass();
Chris@12 120
Chris@12 121 return 'Symfony\\Component\\Finder\\SplFileInfo' === $parent;
Chris@12 122 }
Chris@0 123 }