Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Refactor Analysis\PhpTypeSystem::getTypeFromValue method logic into n…
Browse files Browse the repository at this point in the history
…ew static method Analysis\TypeId::fromValue
  • Loading branch information
TimeToogo committed Apr 7, 2015
1 parent 2b54de7 commit 4d89a61
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
27 changes: 1 addition & 26 deletions Source/Analysis/PhpTypeSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,7 @@ public function getCommonAncestorType(IType $type, IType $otherType)

public function getTypeFromValue($value)
{
switch (gettype($value)) {
case 'string':
return $this->nativeTypes[INativeType::TYPE_STRING];

case 'integer':
return $this->nativeTypes[INativeType::TYPE_INT];

case 'boolean':
return $this->nativeTypes[INativeType::TYPE_BOOL];

case 'double':
return $this->nativeTypes[INativeType::TYPE_DOUBLE];

case 'NULL':
return $this->nativeTypes[INativeType::TYPE_NULL];

case 'array':
return $this->nativeTypes[INativeType::TYPE_ARRAY];

case 'resource':
case 'unknown type':
return $this->nativeTypes[INativeType::TYPE_RESOURCE];

case 'object':
return $this->getObjectType(get_class($value));
}
return $this->getType(TypeId::fromValue($value));
}

public function getTypeFromTypeHint($typeHint)
Expand Down
34 changes: 34 additions & 0 deletions Source/Analysis/TypeId.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Pinq\Analysis;

use Pinq\PinqException;

/**
* Static helper to generate and interpret type identifiers.
*
Expand Down Expand Up @@ -43,4 +45,36 @@ public static function getComposedTypeIdsFromId($compositeId)
{
return explode('|', substr($compositeId, strlen('composite<'), -strlen('>')));
}

public static function fromValue($value)
{
switch (gettype($value)) {
case 'string':
return INativeType::TYPE_STRING;

case 'integer':
return INativeType::TYPE_INT;

case 'boolean':
return INativeType::TYPE_BOOL;

case 'double':
return INativeType::TYPE_DOUBLE;

case 'NULL':
return INativeType::TYPE_NULL;

case 'array':
return INativeType::TYPE_ARRAY;

case 'resource':
case 'unknown type':
return INativeType::TYPE_RESOURCE;

case 'object':
return self::getObject(get_class($value));
}

throw new PinqException('Unknown variable type %s given', gettype($value));
}
}
2 changes: 1 addition & 1 deletion Tests/Integration/Analysis/TypeSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testTypeValueResolution()
$values = [
INativeType::TYPE_STRING => 'abc',
INativeType::TYPE_INT => -34,
INativeType::TYPE_BOOL => true,
INativeType::TYPE_BOOL => true,
INativeType::TYPE_DOUBLE => -4.2454,
INativeType::TYPE_NULL => null,
INativeType::TYPE_ARRAY => [222, ''],
Expand Down

0 comments on commit 4d89a61

Please sign in to comment.