Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CodeCheck.php
Go to the documentation of this file.
1 <?php
8 
12 class CodeCheck
13 {
21  public static function isClassUsed($className, $content)
22  {
23  /* avoid matching namespace instead of class */
24  $content = preg_replace('/namespace[^;]+;/', '', $content);
25  return self::_isRegexpMatched('/[^a-z\d_\$]' . preg_quote($className, '/') . '[^a-z\d_\\\\]/iS', $content);
26  }
27 
35  public static function isNamespaceUsed($namespace, $content)
36  {
37  return self::_isRegexpMatched('/namespace\s+' . preg_quote($namespace, '/') . ';/S', $content)
38  || self::_isRegexpMatched('/[^a-zA-Z\d_]' . preg_quote($namespace . '\\', '/') . '/S', $content);
39  }
40 
57  public static function isFunctionCalled($method, $content, $class = null)
58  {
59  $quotedMethod = preg_quote($method, '/');
60  if (!$class) {
62  '/(?<![a-z\d_:]|->|function\s)' . $quotedMethod . '\s*\(/iS',
63  $content
64  );
65  }
66  // without opening parentheses to match static callbacks notation
67  if (self::_isRegexpMatched(
68  '/' . preg_quote($class, '/') . '::\s*' . $quotedMethod . '[^a-z\d_]/iS',
69  $content
70  )
71  ) {
72  return true;
73  }
74  if (self::isClassOrInterface($content, $class) || self::isDirectDescendant($content, $class)) {
75  return self::_isRegexpMatched('/this->' . $quotedMethod . '\s*\(/iS', $content)
77  '/(self|static|parent)::\s*' . $quotedMethod . '\s*\(/iS',
78  $content
79  );
80  }
81  }
82 
93  public static function isPropertyUsed($property, $content, $class = null)
94  {
95  if ($class) {
96  if (!self::isClassOrInterface($content, $class) && !self::isDirectDescendant($content, $class)) {
97  return false;
98  }
99  }
100  return self::_isRegexpMatched(
101  '/[^a-z\d_]' . preg_quote($property, '/') . '[^a-z\d_]/iS',
102  $content
103  );
104  }
105 
113  public static function isClassOrInterface($content, $name)
114  {
115  return self::_isRegexpMatched('/\b(?:class|interface)\s+' . preg_quote($name, '/') . '\b[^{]*\{/iS', $content);
116  }
117 
125  public static function isDirectDescendant($content, $name)
126  {
127  $name = preg_quote($name, '/');
128  return self::_isRegexpMatched(
129  '/\s+extends\s+\\\\?' . $name . '\b|\s+implements\s+[^{]*\b' . $name . '\b[^{^\\\\]*\{/iS',
130  $content
131  );
132  }
133 
142  protected static function _isRegexpMatched($regexp, $content)
143  {
144  $result = preg_match($regexp, $content);
145  if ($result === false) {
146  throw new \Exception('An error occurred when matching regexp "' . $regexp . '""');
147  }
148  return 1 === $result;
149  }
150 }
static isClassUsed($className, $content)
Definition: CodeCheck.php:21
static isPropertyUsed($property, $content, $class=null)
Definition: CodeCheck.php:93
static isFunctionCalled($method, $content, $class=null)
Definition: CodeCheck.php:57
static isDirectDescendant($content, $name)
Definition: CodeCheck.php:125
$_option $_optionId $class
Definition: date.phtml:13
static _isRegexpMatched($regexp, $content)
Definition: CodeCheck.php:142
static isClassOrInterface($content, $name)
Definition: CodeCheck.php:113
$method
Definition: info.phtml:13
static isNamespaceUsed($namespace, $content)
Definition: CodeCheck.php:35
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14