-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to class builder instead of shims (#1)
* Switch to class builder instead of shims * Fix sf6; fix ci * Improve class builder * CI fix for CC * Small improvement * Enable xdebug coverage; small fixes
- Loading branch information
Showing
14 changed files
with
302 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
/.gitattributes export-ignore | ||
/.github export-ignore | ||
/.gitignore export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/phpunit6.xml.dist export-ignore | ||
/phpunit7.xml.dist export-ignore | ||
/phpunit8.xml.dist export-ignore | ||
/phpunit9.xml.dist export-ignore | ||
/tests export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ vendor/ | |
*.cache | ||
*.iml | ||
composer.lock | ||
phpunit.xml | ||
phpunit*.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"> | ||
<testsuites> | ||
<testsuite name="All Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
<exclude> | ||
<directory suffix=".php">./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd"> | ||
<testsuites> | ||
<testsuite name="All Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
<exclude> | ||
<directory suffix=".php">./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.0/phpunit.xsd"> | ||
<testsuites> | ||
<testsuite name="All Tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
<exclude> | ||
<directory suffix=".php">./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
namespace uuf6429\ExpressionLanguage; | ||
|
||
use JetBrains\PhpStorm\Language; | ||
use ReflectionException; | ||
use ReflectionMethod; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
class ClassBuilder | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $parent; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $child; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $usedTraits = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $usedImports = []; | ||
|
||
/** | ||
* @var array<string, string> | ||
*/ | ||
private $overriddenMethods = []; | ||
|
||
public static function create(): ClassBuilder | ||
{ | ||
return new self(); | ||
} | ||
|
||
public function class(string $fqn): ClassBuilder | ||
{ | ||
$this->child = '\\' . ltrim($fqn, '\\'); | ||
return $this; | ||
} | ||
|
||
public function extend(string $fqn): ClassBuilder | ||
{ | ||
$this->parent = '\\' . ltrim($fqn, '\\'); | ||
return $this; | ||
} | ||
|
||
public function use(string $fqn): ClassBuilder | ||
{ | ||
$this->usedTraits[] = '\\' . ltrim($fqn, '\\'); | ||
return $this; | ||
} | ||
|
||
public function import(string $fqn): ClassBuilder | ||
{ | ||
$this->usedImports[] = '\\' . ltrim($fqn, '\\'); | ||
return $this; | ||
} | ||
|
||
public function override( | ||
string $method, | ||
#[Language('InjectablePHP')] | ||
string $body | ||
): ClassBuilder { | ||
$this->overriddenMethods[$method] = $body; | ||
return $this; | ||
} | ||
|
||
public function buildClass(): string | ||
{ | ||
|
||
list($class, $namespace) = array_map('strrev', explode('\\', strrev($this->child), 2)) + ['']; | ||
|
||
$codeLines = [ | ||
'', | ||
sprintf('namespace %s;', ltrim($namespace, '\\')), | ||
'', | ||
]; | ||
|
||
foreach ($this->usedImports as $import) { | ||
$codeLines[] = "use $import;"; | ||
} | ||
|
||
$codeLines[] = ''; | ||
|
||
$codeLines[] = "class $class extends $this->parent"; | ||
$codeLines[] = '{'; | ||
|
||
foreach ($this->usedTraits as $trait) { | ||
$codeLines[] = " use $trait;"; | ||
} | ||
|
||
foreach ($this->overriddenMethods as $method => $body) { | ||
if (($sig = $this->extractSignature($this->parent, $method)) !== null) { | ||
$codeLines[] = ''; | ||
array_push($codeLines, ...$sig); | ||
$codeLines[] = ' {'; | ||
$codeLines[] = " $body"; | ||
$codeLines[] = ' }'; | ||
} | ||
} | ||
|
||
$codeLines[] = '}'; | ||
|
||
return implode("\n", $codeLines); | ||
} | ||
|
||
public function createClass() | ||
{ | ||
eval($this->buildClass()); | ||
} | ||
|
||
/** | ||
* @param string $class | ||
* @param string $method | ||
* @return string[]|null | ||
*/ | ||
private function extractSignature(string $class, string $method) | ||
{ | ||
try { | ||
$code = file_get_contents( | ||
(new ReflectionMethod($class, $method))->getFileName() | ||
); | ||
|
||
return preg_match("/([^}]+function {$method}[^{]+?)\\n\\s+?{/", $code, $matches) | ||
? array_filter(explode("\n", $matches[1])) | ||
: null; | ||
} catch (ReflectionException $ex) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.