-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathFile.php
More file actions
108 lines (96 loc) · 2.21 KB
/
Copy pathFile.php
File metadata and controls
108 lines (96 loc) · 2.21 KB
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
<?php
namespace Psecio\Parse;
use SplFileInfo;
use RuntimeException;
use PhpParser\Node;
/**
* Wrapper around an SplFileInfo object
*/
class File
{
/**
* @var SplFileInfo Info about this file
*/
private $splFileInfo;
/**
* @var string[] File contents
*/
private $lines;
/**
* Set the SplFileInfo object
*
* @param SplFileInfo $splFileInfo
* @throws RuntimeException If file does not exist
*/
public function __construct(SplFileInfo $splFileInfo)
{
if (!$splFileInfo->isReadable()) {
throw new RuntimeException("Failed to open file '{$splFileInfo->getRealPath()}'");
}
$this->splFileInfo = $splFileInfo;
$this->lines = file($splFileInfo->getRealPath(), FILE_IGNORE_NEW_LINES);
}
/**
* Get to SplFileInfo object
*
* @return SplFileInfo
*/
public function getSplFileInfo()
{
return $this->splFileInfo;
}
/**
* Get the file path
*
* @return string
*/
public function getPath()
{
return $this->getSplFileInfo()->getPathname();
}
/**
* Test if path matches a regular expression
*
* @param string $regexp
* @return bool
*/
public function isPathMatch($regexp)
{
return !!preg_match($regexp, $this->getPath());
}
/**
* Get the file contents
*
* @return string File contents
*/
public function getContents()
{
return implode("\n", $this->lines);
}
/**
* Pull out given lines from file contents
*
* @param integer $startLine
* @param integer $endLine
* @return string[]
*/
public function fetchLines($startLine, $endLine = 0)
{
$startLine--;
$endLine = $endLine ?: $startLine;
$length = $endLine - $startLine;
$length = $length ?: 1;
return array_slice($this->lines, $startLine, $length);
}
/**
* Fetch Node line contents
*
* @param Node $node
* @return string[]
*/
public function fetchNode(Node $node)
{
$attr = $node->getAttributes();
return $this->fetchLines($attr['startLine'], $attr['endLine']);
}
}