PhpCsFixer\Tokenizer\Tokens::isMonolithicPhp PHP Method

isMonolithicPhp() public method

Checks that the code is pure PHP code, in a single code block, starting with an open tag.
public isMonolithicPhp ( ) : boolean
return boolean
    public function isMonolithicPhp()
    {
        $size = $this->count();
        if (0 === $size) {
            return false;
        }
        // If code is not monolithic there is a great chance that first or last token is `T_INLINE_HTML`:
        if ($this[0]->isGivenKind(T_INLINE_HTML) || $this[$size - 1]->isGivenKind(T_INLINE_HTML)) {
            return false;
        }
        for ($index = 1; $index < $size; ++$index) {
            if ($this[$index]->isGivenKind(array(T_INLINE_HTML, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO)) || defined('HHVM_VERSION') && $this[$index]->equals(array(T_ECHO, '<?='))) {
                return false;
            }
        }
        return true;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $lineEnding = $this->whitespacesConfig->getLineEnding();
     // ignore files with short open tag and ignore non-monolithic files
     if (!$tokens[0]->isGivenKind(T_OPEN_TAG) || !$tokens->isMonolithicPhp()) {
         return;
     }
     $newlineFound = false;
     /** @var Token $token */
     foreach ($tokens as $token) {
         if ($token->isWhitespace() && false !== strpos($token->getContent(), "\n")) {
             $newlineFound = true;
             break;
         }
     }
     // ignore one-line files
     if (!$newlineFound) {
         return;
     }
     $token = $tokens[0];
     if (false === strpos($token->getContent(), "\n")) {
         $token->setContent(rtrim($token->getContent()) . $lineEnding);
     }
     if (!$tokens[1]->isWhitespace() && false === strpos($tokens[1]->getContent(), "\n")) {
         $tokens->insertAt(1, new Token(array(T_WHITESPACE, $lineEnding)));
     }
 }
All Usage Examples Of PhpCsFixer\Tokenizer\Tokens::isMonolithicPhp