예제 #1
0
 /**
  * Run Action
  *
  * @param \PHP\Manipulator\TokenContainer $container
  */
 public function run(TokenContainer $container)
 {
     $this->_container = $container;
     $iterator = $container->getIterator();
     $insideClassOrInterface = false;
     $classLevel = null;
     $level = 0;
     $insideMethod = false;
     $methodLevel = null;
     while ($iterator->valid()) {
         $token = $iterator->current();
         if ($this->isOpeningCurlyBrace($token)) {
             $level++;
         }
         if ($this->isClosingCurlyBrace($token)) {
             $level--;
             if ($classLevel === $level && true === $insideClassOrInterface) {
                 $insideClassOrInterface = false;
                 $classLevel = null;
                 if (true === $insideMethod) {
                     $insideMethod = false;
                     $methodLevel = null;
                 }
             }
         }
         if ($this->isType($token, array(T_CLASS, T_INTERFACE))) {
             $insideClassOrInterface = true;
             $classLevel = $level;
         }
         if (true === $insideClassOrInterface && false === $insideMethod) {
             if ($this->isType($token, T_FUNCTION)) {
                 $insideMethod = true;
                 if (!$this->isPrecededByTokenType($iterator, array(T_PUBLIC, T_PRIVATE, T_PROTECTED))) {
                     $token = $iterator->current();
                     $publicToken = new Token('public', T_PUBLIC);
                     $whitespaceToken = new Token(' ', T_WHITESPACE);
                     $this->_container->insertTokensBefore($token, array($publicToken, $whitespaceToken));
                     $iterator->update($token);
                 }
             }
         }
         $iterator->next();
     }
     $container->retokenize();
 }
예제 #2
0
 /**
  * @covers \PHP\Manipulator\TokenContainer::insertTokensBefore
  * @covers \Exception
  */
 public function testInsertTokensAfterThrowsExceptionIfBeforeTokenNotExists()
 {
     $token1 = Token::factory('Token1');
     $token2 = Token::factory('Token2');
     $token3 = Token::factory('Token3');
     $token4 = Token::factory('Token4');
     $container = new TokenContainer(array($token1));
     try {
         $container->insertTokensBefore($token2, array($token3, $token4));
         $this->fail('Expected exception not thrown');
     } catch (\Exception $e) {
         $this->assertEquals("Container does not contain Token: {$token2}", $e->getMessage(), 'Wrong exception message');
     }
 }