comparison vendor/psy/psysh/src/Command/WhereamiCommand.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
31 * @param null|string $colorMode (default: null) 31 * @param null|string $colorMode (default: null)
32 */ 32 */
33 public function __construct($colorMode = null) 33 public function __construct($colorMode = null)
34 { 34 {
35 $this->colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO; 35 $this->colorMode = $colorMode ?: Configuration::COLOR_MODE_AUTO;
36 $this->backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); 36 $this->backtrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
37 37
38 parent::__construct(); 38 parent::__construct();
39 } 39 }
40 40
41 /** 41 /**
67 * 67 *
68 * @return array 68 * @return array
69 */ 69 */
70 protected function trace() 70 protected function trace()
71 { 71 {
72 foreach (array_reverse($this->backtrace) as $stackFrame) { 72 foreach (\array_reverse($this->backtrace) as $stackFrame) {
73 if ($this->isDebugCall($stackFrame)) { 73 if ($this->isDebugCall($stackFrame)) {
74 return $stackFrame; 74 return $stackFrame;
75 } 75 }
76 } 76 }
77 77
78 return end($this->backtrace); 78 return \end($this->backtrace);
79 } 79 }
80 80
81 private static function isDebugCall(array $stackFrame) 81 private static function isDebugCall(array $stackFrame)
82 { 82 {
83 $class = isset($stackFrame['class']) ? $stackFrame['class'] : null; 83 $class = isset($stackFrame['class']) ? $stackFrame['class'] : null;
84 $function = isset($stackFrame['function']) ? $stackFrame['function'] : null; 84 $function = isset($stackFrame['function']) ? $stackFrame['function'] : null;
85 85
86 return ($class === null && $function === 'Psy\debug') || 86 return ($class === null && $function === 'Psy\debug') ||
87 ($class === 'Psy\Shell' && in_array($function, ['__construct', 'debug'])); 87 ($class === 'Psy\Shell' && \in_array($function, ['__construct', 'debug']));
88 } 88 }
89 89
90 /** 90 /**
91 * Determine the file and line based on the specific backtrace. 91 * Determine the file and line based on the specific backtrace.
92 * 92 *
93 * @return array 93 * @return array
94 */ 94 */
95 protected function fileInfo() 95 protected function fileInfo()
96 { 96 {
97 $stackFrame = $this->trace(); 97 $stackFrame = $this->trace();
98 if (preg_match('/eval\(/', $stackFrame['file'])) { 98 if (\preg_match('/eval\(/', $stackFrame['file'])) {
99 preg_match_all('/([^\(]+)\((\d+)/', $stackFrame['file'], $matches); 99 \preg_match_all('/([^\(]+)\((\d+)/', $stackFrame['file'], $matches);
100 $file = $matches[1][0]; 100 $file = $matches[1][0];
101 $line = (int) $matches[2][0]; 101 $line = (int) $matches[2][0];
102 } else { 102 } else {
103 $file = $stackFrame['file']; 103 $file = $stackFrame['file'];
104 $line = $stackFrame['line']; 104 $line = $stackFrame['line'];
105 } 105 }
106 106
107 return compact('file', 'line'); 107 return \compact('file', 'line');
108 } 108 }
109 109
110 /** 110 /**
111 * {@inheritdoc} 111 * {@inheritdoc}
112 */ 112 */
115 $info = $this->fileInfo(); 115 $info = $this->fileInfo();
116 $num = $input->getOption('num'); 116 $num = $input->getOption('num');
117 $factory = new ConsoleColorFactory($this->colorMode); 117 $factory = new ConsoleColorFactory($this->colorMode);
118 $colors = $factory->getConsoleColor(); 118 $colors = $factory->getConsoleColor();
119 $highlighter = new Highlighter($colors); 119 $highlighter = new Highlighter($colors);
120 $contents = file_get_contents($info['file']); 120 $contents = \file_get_contents($info['file']);
121 121
122 $output->startPaging(); 122 $output->startPaging();
123 $output->writeln(''); 123 $output->writeln('');
124 $output->writeln(sprintf('From <info>%s:%s</info>:', $this->replaceCwd($info['file']), $info['line'])); 124 $output->writeln(\sprintf('From <info>%s:%s</info>:', $this->replaceCwd($info['file']), $info['line']));
125 $output->writeln(''); 125 $output->writeln('');
126 $output->write($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW); 126 $output->write($highlighter->getCodeSnippet($contents, $info['line'], $num, $num), ShellOutput::OUTPUT_RAW);
127 $output->stopPaging(); 127 $output->stopPaging();
128 } 128 }
129 129
134 * 134 *
135 * @return string 135 * @return string
136 */ 136 */
137 private function replaceCwd($file) 137 private function replaceCwd($file)
138 { 138 {
139 $cwd = getcwd(); 139 $cwd = \getcwd();
140 if ($cwd === false) { 140 if ($cwd === false) {
141 return $file; 141 return $file;
142 } 142 }
143 143
144 $cwd = rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; 144 $cwd = \rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
145 145
146 return preg_replace('/^' . preg_quote($cwd, '/') . '/', '', $file); 146 return \preg_replace('/^' . \preg_quote($cwd, '/') . '/', '', $file);
147 } 147 }
148 } 148 }