Merge pull request #14266 from seamuslee001/dev_core_369
[civicrm-core.git] / CRM / Core / CodeGen / BaseTask.php
index f220415bd014b79fa6e2df2bcba36e25f379dea3..c58a32ac15aa06b6fd140bcfd522402b37549c6a 100644 (file)
@@ -37,4 +37,48 @@ abstract class CRM_Core_CodeGen_BaseTask implements CRM_Core_CodeGen_ITask {
     return TRUE;
   }
 
+  /**
+   * Extract a single regex from a file.
+   *
+   * @param string $file
+   *   File name
+   * @param string $regex
+   *   A pattern to match. Ex: "foo=([a-z]+)".
+   * @return string|NULL
+   *   The value matched.
+   */
+  protected static function extractRegex($file, $regex) {
+    $content = file_get_contents($file);
+    if (preg_match($regex, $content, $matches)) {
+      return $matches[1];
+    }
+    else {
+      return NULL;
+    }
+  }
+
+  /**
+   * Determine if two snippets of PHP code are approximately equivalent.
+   *
+   * This includes exceptions to equivalence for (a) whitespace and (b)
+   * the token "GenCodeChecksum".
+   *
+   * This is useful for determining if someone has manually mucked with
+   * one the files. However, it's not perfect -- because whitespace changes
+   * are not detected. Hence, it's good to use in combination with another
+   * heuristic.
+   *
+   * @param $actual
+   * @param $expected
+   * @return bool
+   */
+  protected function isApproxPhpMatch($actual, $expected) {
+    foreach (['actual', 'expected'] as $var) {
+      $$var = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($$var);
+      $$var = preg_replace(';\(GenCodeChecksum:([a-zA-Z0-9]+)\);', '', $$var);
+      $$var = strtolower(preg_replace(';[ \r\n\t];', '', $$var));
+    }
+    return $actual === $expected;
+  }
+
 }