Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Codeception/AspectMock
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Mar 20, 2018
2 parents 3a641ba + 312b167 commit 3a4da1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"require": {
"php": ">=5.6.0",
"goaop/framework": "^2.0.0",
"goaop/framework": "^2.2.0",
"symfony/finder": "~2.4|~3.0|~4.0"
},
"require-dev": {
Expand Down
47 changes: 20 additions & 27 deletions src/AspectMock/Intercept/BeforeMockTransformer.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace AspectMock\Intercept;
use Go\Aop\Aspect;
use Go\Instrument\Transformer\StreamMetaData;
use Go\Instrument\Transformer\WeavingTransformer;
use Go\ParserReflection\ReflectionFile;
use Go\ParserReflection\ReflectionMethod;

class BeforeMockTransformer extends WeavingTransformer
{
Expand All @@ -11,12 +13,9 @@ class BeforeMockTransformer extends WeavingTransformer

public function transform(StreamMetaData $metadata)
{
$fileName = $metadata->uri;

$reflectedFile = new ReflectionFile($fileName);
$namespaces = $reflectedFile->getFileNamespaces();

$dataArray = explode("\n", $metadata->source);
$result = self::RESULT_ABSTAIN;
$reflectedFile = new ReflectionFile($metadata->uri, $metadata->syntaxTree);
$namespaces = $reflectedFile->getFileNamespaces();

foreach ($namespaces as $namespace) {

Expand All @@ -29,10 +28,11 @@ public function transform(StreamMetaData $metadata)
}

// Look for aspects
if (in_array('Go\Aop\Aspect', $class->getInterfaceNames())) {
if (in_array(Aspect::class, $class->getInterfaceNames())) {
continue;
}

/** @var ReflectionMethod[] $methods */
$methods = $class->getMethods();
foreach ($methods as $method) {
if ($method->getDeclaringClass()->name != $class->getName()) {
Expand All @@ -49,8 +49,8 @@ public function transform(StreamMetaData $metadata)
? $this->beforeStatic
: $this->before;

// replace return with yield when doccomment shows it returns a Generator
if (preg_match('/(\@return\s+[\\\]?Generator)/', $method->getDocComment())) {
// replace return with yield when method is Generator
if ($method->isGenerator()) {
$beforeDefinition = str_replace('return', 'yield', $beforeDefinition);
}
if (method_exists($method, 'getReturnType') && $method->getReturnType() == 'void') {
Expand All @@ -65,28 +65,21 @@ public function transform(StreamMetaData $metadata)
foreach ($reflectedParams as $reflectedParam) {
$params[] = ($reflectedParam->isPassedByReference() ? '&$' : '$') . $reflectedParam->getName();
}
$params = implode(", ", $params);
$params = implode(", ", $params);
$beforeDefinition = sprintf($beforeDefinition, $params);
for ($i = $method->getStartLine() - 1; $i < $method->getEndLine(); $i++) {
$pos = strpos($dataArray[$i], '{');
if ($pos === false) {
continue;
} else {
// Bug FIX for functions that have the curly bracket as default on their own parameters:
// Launch a "continue" command if the bracket found have a quote (') or a double quote (")
// exactly just before or after
if (in_array(substr($dataArray[$i], $pos - 1, 1), ['"', "'"]) ||
in_array(substr($dataArray[$i], $pos + 1, 1), ['"', "'"])
) {
continue;
}
$tokenPosition = $method->getNode()->getAttribute('startTokenPos');
do {
if ($metadata->tokenStream[$tokenPosition][1] === '{') {
$metadata->tokenStream[$tokenPosition][1] .= $beforeDefinition;
$result = self::RESULT_TRANSFORMED;
break;
}
$dataArray[$i] = substr($dataArray[$i], 0, $pos + 1) . $beforeDefinition . substr($dataArray[$i], $pos + 1);
break;
}
$tokenPosition++;
} while (isset($metadata->tokenStream[$tokenPosition]));
}
}
}
$metadata->source = implode("\n", $dataArray);

return $result;
}
}

0 comments on commit 3a4da1c

Please sign in to comment.