/usr/share/php/TokenReflection/Stream/StreamBase.php is in php-tokenreflection 1.4.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | <?php
/**
* PHP Token Reflection
*
* Version 1.4.0
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this library in the file LICENSE.md.
*
* @author Ondřej Nešpor
* @author Jaroslav Hanslík
*/
namespace TokenReflection\Stream;
use TokenReflection\Exception;
use SeekableIterator, Countable, ArrayAccess, Serializable;
// Ensure that we check if we have a native support of traits
if (!defined('NATIVE_TRAITS')) {
require_once __DIR__ . '/../Broker.php';
}
/**
* Token stream iterator base class.
*/
abstract class StreamBase implements SeekableIterator, Countable, ArrayAccess, Serializable
{
/**
* Token source file name.
*
* @var string
*/
protected $fileName = 'unknown';
/**
* Tokens storage.
*
* @var array
*/
private $tokens = array();
/**
* Internal pointer.
*
* @var integer
*/
private $position = 0;
/**
* Token stream size.
*
* @var integer
*/
private $count = 0;
/**
* Constructor.
*
* Protected to ensure that the concrete implementation will override it.
*
* @throws \TokenReflection\Exception\StreamException If tokenizer PHP extension is missing.
*/
protected function __construct()
{
if (!extension_loaded('tokenizer')) {
throw new Exception\StreamException($this, 'The tokenizer PHP extension is not loaded.', Exception\StreamException::PHP_EXT_MISSING);
}
}
/**
* Extracts tokens from a source code.
*
* @param string $source Source code
*/
protected final function processSource($source)
{
$stream = @token_get_all(str_replace(array("\r\n", "\r"), "\n", $source));
static $checkLines = array(T_COMMENT => true, T_WHITESPACE => true, T_DOC_COMMENT => true, T_INLINE_HTML => true, T_ENCAPSED_AND_WHITESPACE => true, T_CONSTANT_ENCAPSED_STRING => true);
foreach ($stream as $position => $token) {
if (is_array($token)) {
if (!NATIVE_TRAITS && T_STRING === $token[0]) {
$lValue = strtolower($token[1]);
if ('trait' === $lValue) {
$token[0] = T_TRAIT;
} elseif ('insteadof' === $lValue) {
$token[0] = T_INSTEADOF;
} elseif ('__TRAIT__' === $token[1]) {
$token[0] = T_TRAIT_C;
} elseif ('callable' === $lValue) {
$token[0] = T_CALLABLE;
}
}
$this->tokens[] = $token;
} else {
$previous = $this->tokens[$position - 1];
$line = $previous[2];
if (isset($checkLines[$previous[0]])) {
$line += substr_count($previous[1], "\n");
}
$this->tokens[] = array($token, $token, $line);
}
}
$this->count = count($this->tokens);
}
/**
* Returns the file name this is a part of.
*
* @return string
*/
public function getFileName()
{
return $this->fileName;
}
/**
* Returns the original source code.
*
* @return string
*/
public function getSource()
{
return $this->getSourcePart();
}
/**
* Returns a part of the source code.
*
* @param mixed $start Start offset
* @param mixed $end End offset
* @return string
*/
public function getSourcePart($start = null, $end = null)
{
$start = (int) $start;
$end = null === $end ? ($this->count - 1) : (int) $end;
$source = '';
for ($i = $start; $i <= $end; $i++) {
$source .= $this->tokens[$i][1];
}
return $source;
}
/**
* Finds the position of the token of the given type.
*
* @param integer|string $type Token type
* @return \TokenReflection\Stream|boolean
*/
public function find($type)
{
$actual = $this->position;
while (isset($this->tokens[$this->position])) {
if ($type === $this->tokens[$this->position][0]) {
return $this;
}
$this->position++;
}
$this->position = $actual;
return false;
}
/**
* Returns the position of the token with the matching bracket.
*
* @return \TokenReflection\Stream
* @throws \TokenReflection\Exception\RuntimeException If out of the token stream.
* @throws \TokenReflection\Exception\RuntimeException If there is no bracket at the current position.
* @throws \TokenReflection\Exception\RuntimeException If the matching bracket could not be found.
*/
public function findMatchingBracket()
{
static $brackets = array(
'(' => ')',
'{' => '}',
'[' => ']',
T_CURLY_OPEN => '}',
T_DOLLAR_OPEN_CURLY_BRACES => '}'
);
if (!$this->valid()) {
throw new Exception\StreamException($this, 'Out of token stream.', Exception\StreamException::READ_BEYOND_EOS);
}
$position = $this->position;
$bracket = $this->tokens[$this->position][0];
if (!isset($brackets[$bracket])) {
throw new Exception\StreamException($this, sprintf('There is no usable bracket at position "%d".', $position), Exception\StreamException::DOES_NOT_EXIST);
}
$searching = $brackets[$bracket];
$level = 0;
while (isset($this->tokens[$this->position])) {
$type = $this->tokens[$this->position][0];
if ($searching === $type) {
$level--;
} elseif ($bracket === $type || ($searching === '}' && ('{' === $type || T_CURLY_OPEN === $type || T_DOLLAR_OPEN_CURLY_BRACES === $type))) {
$level++;
}
if (0 === $level) {
return $this;
}
$this->position++;
}
throw new Exception\StreamException($this, sprintf('Could not find the end bracket "%s" of the bracket at position "%d".', $searching, $position), Exception\StreamException::DOES_NOT_EXIST);
}
/**
* Skips whitespaces and comments next to the current position.
*
* @param boolean $skipDocBlocks Skip docblocks as well
* @return \TokenReflection\Stream\StreamBase
*/
public function skipWhitespaces($skipDocBlocks = false)
{
static $skipped = array(T_WHITESPACE => true, T_COMMENT => true, T_DOC_COMMENT => true);
do {
$this->position++;
} while (isset($this->tokens[$this->position]) && isset($skipped[$this->tokens[$this->position][0]]) && ($skipDocBlocks || $this->tokens[$this->position][0] !== T_DOC_COMMENT));
return $this;
}
/**
* Returns if the token stream is at a whitespace position.
*
* @param boolean $docBlock Consider docblocks as whitespaces
* @return boolean
*/
public function isWhitespace($docBlock = false)
{
static $skipped = array(T_WHITESPACE => true, T_COMMENT => true, T_DOC_COMMENT => false);
if (!$this->valid()) {
return false;
}
return $docBlock ? isset($skipped[$this->getType()]) : !empty($skipped[$this->getType()]);
}
/**
* Checks if there is a token of the given type at the given position.
*
* @param integer|string $type Token type
* @param integer $position Position; if none given, consider the current iteration position
* @return boolean
*/
public function is($type, $position = -1)
{
return $type === $this->getType($position);
}
/**
* Returns the type of a token.
*
* @param integer $position Token position; if none given, consider the current iteration position
* @return string|integer|null
*/
public function getType($position = -1)
{
if (-1 === $position) {
$position = $this->position;
}
return isset($this->tokens[$position]) ? $this->tokens[$position][0] : null;
}
/**
* Returns the current token value.
*
* @param integer $position Token position; if none given, consider the current iteration position
* @return stirng
*/
public function getTokenValue($position = -1)
{
if (-1 === $position) {
$position = $this->position;
}
return isset($this->tokens[$position]) ? $this->tokens[$position][1] : null;
}
/**
* Returns the token type name.
*
* @param integer $position Token position; if none given, consider the current iteration position
* @return string|null
*/
public function getTokenName($position = -1)
{
$type = $this->getType($position);
if (is_string($type)) {
return $type;
} elseif (T_TRAIT === $type) {
return 'T_TRAIT';
} elseif (T_INSTEADOF === $type) {
return 'T_INSTEADOF';
} elseif (T_CALLABLE === $type) {
return 'T_CALLABLE';
}
return token_name($type);
}
/**
* Stream serialization.
*
* @return string
*/
public function serialize()
{
return serialize(array($this->fileName, $this->tokens));
}
/**
* Restores the stream from the serialized state.
*
* @param string $serialized Serialized form
* @throws \TokenReflection\Exception\StreamException On deserialization error.
*/
public function unserialize($serialized)
{
$data = @unserialize($serialized);
if (false === $data) {
throw new Exception\StreamException($this, 'Could not deserialize the serialized data.', Exception\StreamException::SERIALIZATION_ERROR);
}
if (2 !== count($data) || !is_string($data[0]) || !is_array($data[1])) {
throw new Exception\StreamException($this, 'Invalid serialization data.', Exception\StreamException::SERIALIZATION_ERROR);
}
$this->fileName = $data[0];
$this->tokens = $data[1];
$this->count = count($this->tokens);
$this->position = 0;
}
/**
* Checks of there is a token with the given index.
*
* @param integer $offset Token index
* @return boolean
*/
public function offsetExists($offset)
{
return isset($this->tokens[$offset]);
}
/**
* Removes a token.
*
* Unsupported.
*
* @param integer $offset Position
* @throws \TokenReflection\Exception\StreamException Unsupported.
*/
public function offsetUnset($offset)
{
throw new Exception\StreamException($this, 'Removing of tokens from the stream is not supported.', Exception\StreamException::UNSUPPORTED);
}
/**
* Returns a token at the given index.
*
* @param integer $offset Token index
* @return mixed
*/
public function offsetGet($offset)
{
return isset($this->tokens[$offset]) ? $this->tokens[$offset] : null;
}
/**
* Sets a value of a particular token.
*
* Unsupported
*
* @param integer $offset Position
* @param mixed $value Value
* @throws \TokenReflection\Exception\StreamException Unsupported.
*/
public function offsetSet($offset, $value)
{
throw new Exception\StreamException($this, 'Setting token values is not supported.', Exception\StreamException::UNSUPPORTED);
}
/**
* Returns the current internal pointer value.
*
* @return integer
*/
public function key()
{
return $this->position;
}
/**
* Advances the internal pointer.
*
* @return \TokenReflection\Stream
*/
public function next()
{
$this->position++;
return $this;
}
/**
* Sets the internal pointer to zero.
*
* @return \TokenReflection\Stream
*/
public function rewind()
{
$this->position = 0;
return $this;
}
/**
* Returns the current token.
*
* @return array|null
*/
public function current()
{
return isset($this->tokens[$this->position]) ? $this->tokens[$this->position] : null;
}
/**
* Checks if there is a token on the current position.
*
* @return boolean
*/
public function valid()
{
return isset($this->tokens[$this->position]);
}
/**
* Returns the number of tokens in the stream.
*
* @return integer
*/
public function count()
{
return $this->count;
}
/**
* Sets the internal pointer to the given value.
*
* @param integer $position New position
* @return \TokenReflection\Stream
*/
public function seek($position)
{
$this->position = (int) $position;
return $this;
}
/**
* Returns the stream source code.
*
* @return string
*/
public function __toString()
{
return $this->getSource();
}
}
|