/** * Run Action * * @param \PHP\Manipulator\TokenContainer $container */ public function run(TokenContainer $container) { $iterator = $container->getIterator(); $waitingForIf = false; $replaceTokens = array(); $allowedTypes = array(T_IF, T_ELSE, T_WHITESPACE); while ($iterator->valid()) { $token = $iterator->current(); if ($this->isType($token, T_ELSE)) { $waitingForIf = true; $replaceTokens = array(); } if (true === $waitingForIf && !$this->isType($token, $allowedTypes)) { $waitingForIf = false; } else { $replaceTokens[] = $token; } if (true === $waitingForIf && $this->isType($token, T_IF)) { $waitingForIf = false; $token = array_pop($replaceTokens); $token->setType(T_ELSEIF); $token->setValue('elseif'); $container->removeTokens($replaceTokens); $replaceTokens = array(); } $iterator->next(); } $container->retokenize(); }
/** * Remove php-code * * @param \PHP\Manipulator\TokenContainer $container * @param mixed $params */ public function run(TokenContainer $container) { $iterator = $container->getIterator(); $open = false; $this->_deleteList = array(); $allowedTokens = array(T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO); while ($iterator->valid()) { $token = $iterator->current(); if ($this->isType($token, $allowedTokens)) { $open = true; } if ($this->_shoudDelete($open)) { $this->_deleteList[] = $token; } if ($this->isType($token, T_CLOSE_TAG)) { $open = false; } $iterator->next(); } $container->removeTokens($this->_deleteList); $container->retokenize(); }
/** * @covers \PHP\Manipulator\TokenContainer::removeTokens */ public function testRemoveTokens() { $token1 = Token::factory('Token1'); $token2 = Token::factory('Token2'); $token3 = Token::factory('Token3'); $container = new TokenContainer(); $container[] = $token1; $container[] = $token2; $container[] = $token3; $fluent = $container->removeTokens(array($token3, $token1)); $this->assertSame($fluent, $container, 'No fluent interface'); $this->assertCount(1, $container, 'Wrong count of Tokens in Container'); $this->assertFalse($container->contains($token1), 'Container contains Token1'); $this->assertFalse($container->contains($token3), 'Container contains Token3'); $this->assertTrue($container->contains($token2), 'Container not contains Token2'); }