PluginProbe
WPIDE – File Manager & Code Editor / 3.1
WPIDE – File Manager & Code Editor v3.1
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / Helpers / PhpValidator.php

PhpValidator.php in WPIDE – File Manager & Code Editor 3.1, at App/Helpers/PhpValidator.php

49 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPIDE\App\Helpers;
3
4 use PhpParser\ParserFactory;
5 use WPIDE\App\Utils\Exception;
6
7 class PhpValidator
8 {
9
10 public static function validate($content)
11 {
12 self::parse($content);
13 self::validateConstants($content);
14 }
15
16 public static function parse($content) {
17
18 $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
19 $parser->parse($content);
20 }
21
22 /**
23 * @throws Exception
24 */
25 protected static function validateConstants($content)
26 {
27
28 $pattern = '/define\((.+?),.+?\)/';
29 if (preg_match_all($pattern, $content, $matches)) {
30
31 $matches = array_map(function ($match) {
32 return trim(str_replace(['"', "'"], "", $match));
33 }, $matches[1]);
34
35 $duplicates = array();
36 foreach (array_count_values($matches) as $val => $c) {
37 if ($c > 1) {
38 $duplicates[] = $val;
39 }
40 }
41
42 if (!empty($duplicates)) {
43 $constant = array_shift($duplicates);
44 throw new Exception('Constant ' . $constant . ' already defined!');
45 }
46 }
47 }
48 }
49