Returns an array of lines from a file, class, or arbitrary string, where $data is the data
to read the lines from and $lines is an array of line numbers specifying which lines should
be read.
public static lines ( string $data, array $lines ) : array | ||
$data | string | If `$data` contains newlines, it will be read from directly, and have its own lines returned. If `$data` is a physical file path, that file will be read and have its lines returned. If `$data` is a class name, it will be converted into a physical file path and read. |
$lines | array | The array of lines to read. If a given line is not present in the data, it will be silently ignored. |
return | array | Returns an array where the keys are matching `$lines`, and the values are the corresponding line numbers in `$data`. |
protected static function _method(array $object, array $data, array $options = array()) { if (!$data) { return array(); } $lines = Inspector::lines($data['file'], range($data['start'], $data['end'])); $object = array('source' => join("\n", $lines)) + $object; $object += array('tags' => isset($data['tags']) ? $data['tags'] : array()); if (isset($object['tags']['return'])) { list($type, $text) = explode(' ', $object['tags']['return'], 2) + array('', ''); $object['return'] = compact('type', 'text'); } return $object; }
/** * Takes an instance of an object (usually a Collection object) containing test * instances. Introspects the test subject classes to extract cyclomatic complexity data. * * @param object $report Instance of Report which is calling apply. * @param array $tests The test to apply this filter on * @param array $options Not used. * @return object|void Returns the instance of `$tests`. */ public static function apply($report, $tests, array $options = array()) { $results = array(); foreach ($tests->invoke('subject') as $class) { $results[$class] = array(); if (!($methods = Inspector::methods($class, 'ranges', array('public' => false)))) { continue; } foreach ($methods as $method => $lines) { $lines = Inspector::lines($class, $lines); $branches = Parser::tokenize(join("\n", (array) $lines), array('include' => static::$_include)); $results[$class][$method] = count($branches) + 1; $report->collect(__CLASS__, $results); } } return $tests; }
<h3 id="source">Source</h3> <div id="sourceCode"></div> <h3>Stack Trace</h3> <div class="lithium-stack-trace"> <ol> <?php foreach ($stack as $id => $frame) { ?> <?php $location = "{$frame['file']}: {$frame['line']}"; $lines = range($frame['line'] - $context, $frame['line'] + $context); $code = Inspector::lines($frame['file'], $lines); ?> <li> <tt><a href="#source" id="<?php echo $id; ?> " class="display-source-excerpt"> <?php echo $frame['functionRef']; ?> </a></tt> <div id="sourceCode<?php echo $id; ?> " style="display: none;">
/** * Locates original location of closures. * * @param mixed $reference File or class name to inspect. * @param integer $callLine Line number of class reference. * @return mixed Returns the line number where the method called is defined. */ protected static function _definition($reference, $callLine) { if (file_exists($reference)) { foreach (array_reverse(token_get_all(file_get_contents($reference))) as $token) { if (!is_array($token) || $token[2] > $callLine) { continue; } if ($token[0] === T_FUNCTION) { return $token[2]; } } return; } list($class, ) = explode('::', $reference); if (!class_exists($class)) { return; } $classRef = new ReflectionClass($class); $methodInfo = Inspector::info($reference); $methodDef = join("\n", Inspector::lines($classRef->getFileName(), range($methodInfo['start'] + 1, $methodInfo['end'] - 1))); foreach (array_reverse(token_get_all("<?php {$methodDef} ?>")) as $token) { if (!is_array($token) || $token[2] > $callLine) { continue; } if ($token[0] === T_FUNCTION) { return $token[2] + $methodInfo['start']; } } }
public function lines($data, $start, $end) { return Inspector::lines($data, range($start, $end)); }
/** * Tests reading specific line numbers of a file that has CRLF line endings. */ public function testLineIntrospectionWithCRLFLineEndings() { $tmpPath = Libraries::get(true, 'resources') . '/tmp/tests/inspector_crlf'; $contents = implode("\r\n", array('one', 'two', 'three', 'four', 'five')); file_put_contents($tmpPath, $contents); $result = Inspector::lines($tmpPath, array(2)); $expected = array(2 => 'two'); $this->assertEqual($expected, $result); $result = Inspector::lines($tmpPath, array(1, 5)); $expected = array(1 => 'one', 5 => 'five'); $this->assertEqual($expected, $result); $this->_cleanUp(); }
/** * Tests reading specific line numbers of a file. * * @return void */ public function testLineIntrospection() { $result = Inspector::lines(__FILE__, array(__LINE__ - 1)); $expected = array(__LINE__ - 2 => "\tpublic function testLineIntrospection() {"); $this->assertEqual($expected, $result); $result = Inspector::lines(__CLASS__, array(14)); $expected = array(14 => 'class InspectorTest extends \\lithium\\test\\Unit {'); $this->assertEqual($expected, $result); $this->expectException('/Missing argument 2/'); $this->assertNull(Inspector::lines('\\lithium\\core\\Foo')); $this->assertNull(Inspector::lines(__CLASS__, array())); }
protected static function _extractFileCode($ref, $options) { $library = Libraries::get($options['library']); $file = $ref['file']; if (!($path = realpath("{$library['path']}/{$file}"))) { return; } list($start, $end) = array_map('intval', explode('-', $ref['lines'])); $lines = Inspector::lines(file_get_contents($path), range($start, $end)); return array("{$ref['file']}::{$ref['lines']}", "\t" . join("\n\t", $lines)); }