Skip to content

Commit 0a53610

Browse files
committed
catch Exception and add some invented type
1 parent b5e8ed1 commit 0a53610

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/Ladybug/Type/Object/Container.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,12 @@ protected function loadClassMethods(\ReflectionClass $reflectedObject)
386386
$methodParameter = new MethodParameter();
387387
$methodParameter->setName($methodParameterReflected->getName());
388388

389-
$class = $methodParameterReflected->getClass();
389+
try {
390+
$class = $methodParameterReflected->getClass();
391+
} catch (\ReflectionException $e) {
392+
// This happens if the Class does not exist
393+
$methodParameter->setType('Wrong_Typehint!');
394+
}
390395

391396
if ($class instanceof \ReflectionClass) {
392397
$methodParameter->setType($class->getName());
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Ladybug\Tests\Type\Object;
4+
5+
use Ladybug\Inspector\InspectorManager;
6+
use Ladybug\Metadata\MetadataResolver;
7+
use Ladybug\Type;
8+
9+
use \Mockery as m;
10+
11+
class BadTypeHintedParameterContainerTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/** @var Type\Object $type */
14+
protected $type;
15+
16+
protected function setUp()
17+
{
18+
$factory = new Type\FactoryType();
19+
$factory->add(new Type\Null(), 'type_null');
20+
21+
$managerInspectorMock = m::mock('Ladybug\Inspector\InspectorManager');
22+
$managerInspectorMock->shouldReceive('get')->andReturn(null);
23+
24+
$metadataResolverMock = m::mock('Ladybug\Metadata\MetadataResolver');
25+
$metadataResolverMock->shouldReceive('has')->andReturn(false);
26+
27+
$this->type = new Type\Object\Container(8, $factory, $managerInspectorMock, $metadataResolverMock);
28+
}
29+
30+
protected function tearDown()
31+
{
32+
$this->type = null;
33+
}
34+
35+
public function testDumperDoesNotCrashOnWrongTypeHints()
36+
{
37+
$var = new Problematic();
38+
$this->type->load($var);
39+
40+
// class info
41+
$this->assertEquals('Ladybug\Tests\Type\Object\Problematic', $this->type->getClassName());
42+
$this->assertEquals(__FILE__, $this->type->getClassFile());
43+
44+
$this->assertCount(2, $this->type->getClassMethods());
45+
46+
$privateMethod = $this->type->getMethodByName('privateFunction');
47+
$this->assertCount(2, $privateMethod->getParameters());
48+
49+
$parameter1 = $privateMethod->getParameterByName('that');
50+
$this->assertNull($parameter1->getType());
51+
$this->assertFalse($parameter1->isReference());
52+
53+
$badTypeHinted = $privateMethod->getParameterByName('badTypeHinted');
54+
$this->assertEquals('Wrong_Typehint!', $badTypeHinted->getType());
55+
$this->assertFalse($badTypeHinted->isReference());
56+
$this->assertInstanceOf('Ladybug\Type\Null', $badTypeHinted->getDefaultValue());
57+
}
58+
}
59+
60+
class Problematic
61+
{
62+
public function publicFunction()
63+
{
64+
$this->privateFunction($this, null);
65+
}
66+
67+
private function privateFunction($that, BadTypeHint $badTypeHinted = null)
68+
{
69+
}
70+
}

0 commit comments

Comments
 (0)