Skip to content

Commit

Permalink
Avoid fishy switch
Browse files Browse the repository at this point in the history
It's completely broken, and code is easier to understand that way.
  • Loading branch information
greg0ire committed Feb 15, 2018
1 parent 2bcb5b2 commit bc94a80
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
21 changes: 21 additions & 0 deletions spec/Prophecy/Doubler/Generator/Node/MethodNodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,25 @@ function it_handles_object_return_type()
$this->setReturnType('object');
$this->getReturnType()->shouldReturn(version_compare(PHP_VERSION, '7.2', '>=') ? 'object' : '\object');
}

function it_handles_type_aliases()
{
$this->setReturnType('double');
$this->getReturnType()->shouldReturn(version_compare(PHP_VERSION, '7.0', '>=') ? 'float' : '\float');

$this->setReturnType('real');
$this->getReturnType()->shouldReturn(version_compare(PHP_VERSION, '7.0', '>=') ? 'float' : '\float');

$this->setReturnType('boolean');
$this->getReturnType()->shouldReturn(version_compare(PHP_VERSION, '7.0', '>=') ? 'bool' : '\bool');

$this->setReturnType('integer');
$this->getReturnType()->shouldReturn(version_compare(PHP_VERSION, '7.0', '>=') ? 'int' : '\int');
}

function it_handles_null_return_type()
{
$this->setReturnType(null);
$this->getReturnType()->shouldReturn(null);
}
}
44 changes: 15 additions & 29 deletions src/Prophecy/Doubler/Generator/Node/MethodNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,36 +119,22 @@ public function hasReturnType()
*/
public function setReturnType($type = null)
{
switch ($type) {
case '':
$this->returnType = null;
break;

case 'double':
case 'real':
$type = 'float';
// intentional fall through

case 'boolean':
$type = 'bool';
// intentional fall through

case 'integer':
$type = 'int';
// intentional fall through

case 'object':
if (version_compare(PHP_VERSION, '7.2', '>=')) {
$this->returnType = $type;
break;
}
// Fall-through to default case for PHP < 7.2

default:
$this->returnType = $this->typeHintReference->isBuiltInReturnTypeHint($type) ?
$type :
'\\' . ltrim($type, '\\');
if ($type === '' || $type === null) {
$this->returnType = null;
return;
}
$typeMap = array(
'double' => 'float',
'real' => 'float',
'boolean' => 'bool',
'integer' => 'int',
);
if (isset($typeMap[$type])) {
$type = $typeMap[$type];
}
$this->returnType = $this->typeHintReference->isBuiltInReturnTypeHint($type) ?
$type :
'\\' . ltrim($type, '\\');
}

public function getReturnType()
Expand Down

0 comments on commit bc94a80

Please sign in to comment.