Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/CodeManipulation/Actions/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ function prependCodeToFunctions($code, $typedVariants = array(), $fillArgRefs =
function getDeclaredReturnType(Source $s, $function)
{
$parenthesis = $s->next(LEFT_ROUND, $function);
$name = $s->next(T_STRING, $function);
if ($name < $parenthesis && ($s->read($name) === '__construct' || $s->read($name) === '__destruct')) {
return 'void';
}
$next = $s->skip(Source::junk(), $s->match($parenthesis));
if ($s->is(T_USE, $next)) {
$next = $s->skip(Source::junk(), $s->match($s->next(LEFT_ROUND, $next)));
Expand Down
8 changes: 6 additions & 2 deletions tests/includes/NamedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ class NamedObject
{
private $name;

function __construct($name)
public function __construct($name)
{
$this->name = $name;
}

function getName()
public function getName()
{
return $this->name;
}

public function __destruct()
{
}
}
32 changes: 32 additions & 0 deletions tests/return-from-constructor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Return from constructor / antecedent/patchwork#213

--FILE--
<?php

use function Patchwork\redefine;

ini_set('zend.assertions', 1);
error_reporting(E_ALL);

$_SERVER['PHP_SELF'] = __FILE__;

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

redefine('NamedObject::__construct', function ($name) {
return $name;
});

try {
new NamedObject('foo');
} catch (Exception $e) {
echo get_class($e), "\n";
}

?>
===DONE===

--EXPECT--
Patchwork\Exceptions\NonNullToVoid
===DONE===
32 changes: 32 additions & 0 deletions tests/return-from-destructor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Return from destructor / antecedent/patchwork#213

--FILE--
<?php

use function Patchwork\redefine;

ini_set('zend.assertions', 1);
error_reporting(E_ALL);

$_SERVER['PHP_SELF'] = __FILE__;

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

redefine('NamedObject::__destruct', function () {
return __CLASS__;
});

try {
new NamedObject('foo');
} catch (Exception $e) {
echo get_class($e), "\n";
}

?>
===DONE===

--EXPECT--
Patchwork\Exceptions\NonNullToVoid
===DONE===
Loading