Skip to content

Commit

Permalink
Quick-fix #64
Browse files Browse the repository at this point in the history
  • Loading branch information
antecedent committed Jul 27, 2017
1 parent 9748e82 commit 5d3556e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/CodeManipulation/Actions/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ function markPreprocessedFiles(&$target)
};
}

function prependCodeToFunctions($code)
function prependCodeToFunctions($code, $skipVoidTyped = true)
{
return function(Source $s) use ($code) {
return function(Source $s) use ($code, $skipVoidTyped) {
foreach ($s->all(T_FUNCTION) as $function) {
# Skip "use function"
$previous = $s->skipBack(Source::junk(), $function);
if ($s->is(T_USE, $previous)) {
continue;
}
if ($skipVoidTyped && isVoidTyped($s, $function)) {
continue;
}
$bracket = $s->next(LEFT_CURLY, $function);
if (Utils\generatorsSupported()) {
# Skip generators
Expand All @@ -51,6 +54,16 @@ function prependCodeToFunctions($code)
};
}

function isVoidTyped(Source $s, $function)
{
$parenthesis = $s->next(LEFT_ROUND, $function);
$next = $s->skip(Source::junk(), $s->match($parenthesis));
if ($s->is(':', $next)) {
return $s->read($s->skip(Source::junk(), $next), 1) === 'void';
}
return false;
}

function wrapUnaryConstructArguments($construct, $wrapper)
{
return function(Source $s) use ($construct, $wrapper) {
Expand Down
16 changes: 16 additions & 0 deletions tests/includes/Php71VoidReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class Foo
{
public $check = false;

public function bar() : void
{
$this->check = true;
}
}

$foo = new Foo;
$foo->bar();

assert($foo->check === true);
22 changes: 22 additions & 0 deletions tests/php71-void-return-type.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
https://github.com/antecedent/patchwork/issues/63

--SKIPIF--
<?php version_compare(PHP_VERSION, "7.1", ">=")
or die("skip because this bug only occurs in PHP 7.1") ?>

--FILE--
<?php

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 1);
error_reporting(E_ALL | E_STRICT);

require __DIR__ . "/../Patchwork.php";
require __DIR__ . "/includes/Php71VoidReturnType.php";

?>
===DONE===

--EXPECT--
===DONE===

0 comments on commit 5d3556e

Please sign in to comment.