Skip to content

Commit ca3f9fa

Browse files
committed
issue #7 - improve exception messages providing the current class
1 parent 02e98a4 commit ca3f9fa

12 files changed

+109
-3
lines changed

src/Element/AbstractAssignedValueElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct($name, $value = null, $access = parent::ACCESS_PUBLI
3131
public function setValue($value)
3232
{
3333
if ($this->getAcceptNonScalarValue() === false && !is_scalar($value) && $value !== null) {
34-
throw new \InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value', gettype($value)));
34+
throw new \InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->getCalledClass()));
3535
}
3636
$this->value = $value;
3737
return $this;

src/Element/AbstractElement.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct($name)
3232
public function setName($name)
3333
{
3434
if (!self::nameIsValid($name)) {
35-
throw new \InvalidArgumentException(sprintf('Name "%s" is invalid, please provide a valid name', $name));
35+
throw new \InvalidArgumentException(sprintf('Name "%s" is invalid when instantiating %s object', $name, $this->getCalledClass()));
3636
}
3737
$this->name = $name;
3838
return $this;
@@ -84,7 +84,7 @@ public function toString($indentation = null)
8484
{
8585
$lines = array(
8686
$this->getToStringDeclaration($indentation),
87-
$this->getToStringBeforeChildren($indentation),
87+
$this->getToStringBeforeChildren($indentation)
8888
);
8989
foreach ($this->getChildren() as $child) {
9090
$lines[] = $this->getChildContent($child, $indentation + ($this->useBracketsForChildren() ? 1 : 0));
@@ -329,4 +329,11 @@ public function getIndentedString($string, $indentation = null)
329329
}
330330
return implode(self::BREAK_LINE_CHAR, $strings);
331331
}
332+
/**
333+
* @return string
334+
*/
335+
final public function getCalledClass()
336+
{
337+
return substr(get_called_class(), strrpos(get_called_class(), '\\') + 1);
338+
}
332339
}

tests/Element/PhpAnnotationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,13 @@ public function testHasContent()
6868

6969
$this->assertTrue($annotation->hasContent());
7070
}
71+
72+
public function testExceptionMessageOnName()
73+
{
74+
try {
75+
new PhpAnnotation(0, '');
76+
} catch (\InvalidArgumentException $e) {
77+
$this->assertSame('Name "0" is invalid when instantiating PhpAnnotation object', $e->getMessage());
78+
}
79+
}
7180
}

tests/Element/PhpClassTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,13 @@ public function testExtendsFromNamespace()
255255

256256
$this->assertSame("class Foo extends \\DOMDocument\n{\n}", $class->toString());
257257
}
258+
259+
public function testExceptionMessageOnName()
260+
{
261+
try {
262+
new PhpClass(0);
263+
} catch (\InvalidArgumentException $e) {
264+
$this->assertSame('Name "0" is invalid when instantiating PhpClass object', $e->getMessage());
265+
}
266+
}
258267
}

tests/Element/PhpConstantTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,22 @@ public function testGetChildrenTypes()
131131

132132
$this->assertSame(array(), $constant->getChildrenTypes());
133133
}
134+
135+
public function testExceptionMessageOnName()
136+
{
137+
try {
138+
new PhpConstant(0);
139+
} catch (\InvalidArgumentException $e) {
140+
$this->assertSame('Name "0" is invalid when instantiating PhpConstant object', $e->getMessage());
141+
}
142+
}
143+
144+
public function testExceptionMessageOnValue()
145+
{
146+
try {
147+
new PhpConstant('Foo', new \stdClass());
148+
} catch (\InvalidArgumentException $e) {
149+
$this->assertSame('Value of type "object" is not a valid scalar value for PhpConstant object', $e->getMessage());
150+
}
151+
}
134152
}

tests/Element/PhpFileTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ public function testAnnotationClassMethodBlockToString()
8181

8282
$this->assertSame("<?php\n/**\n * date is the key\n * time is the core key\n */\nclass Foo\n{\n public function Bar()\n {\n }\n}\n", $file->toString());
8383
}
84+
85+
public function testExceptionMessageOnName()
86+
{
87+
try {
88+
new PhpFile(0);
89+
} catch (\InvalidArgumentException $e) {
90+
$this->assertSame('Name "0" is invalid when instantiating PhpFile object', $e->getMessage());
91+
}
92+
}
8493
}

tests/Element/PhpFunctionParameterTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@ public function testToStringWithNamespace()
5555

5656
$this->assertSame('My\Name\Space $foo = null', $functionParameter->toString());
5757
}
58+
59+
public function testExceptionMessageOnName()
60+
{
61+
try {
62+
new PhpFunctionParameter(0);
63+
} catch (\InvalidArgumentException $e) {
64+
$this->assertSame('Name "0" is invalid when instantiating PhpFunctionParameter object', $e->getMessage());
65+
}
66+
}
5867
}

tests/Element/PhpFunctionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,13 @@ public function testToStringWithBody()
114114

115115
$this->assertSame("function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true)\n{\n \$bar = 1;\n return \$bar;\n}", $function->toString());
116116
}
117+
118+
public function testExceptionMessageOnName()
119+
{
120+
try {
121+
new PhpFunction(0);
122+
} catch (\InvalidArgumentException $e) {
123+
$this->assertSame('Name "0" is invalid when instantiating PhpFunction object', $e->getMessage());
124+
}
125+
}
117126
}

tests/Element/PhpInterfaceTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,13 @@ public function testSimpleIntefaceEmptyPublicMethodToString()
9797

9898
$this->assertSame("interface Foo\n{\n public function bar();\n}", $interface->toString());
9999
}
100+
101+
public function testExceptionMessageOnName()
102+
{
103+
try {
104+
new PhpInterface(0);
105+
} catch (\InvalidArgumentException $e) {
106+
$this->assertSame('Name "0" is invalid when instantiating PhpInterface object', $e->getMessage());
107+
}
108+
}
100109
}

tests/Element/PhpMethodTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,13 @@ public function testPublicWithBodyToString()
186186

187187
$this->assertSame("public function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true)\n{\n \$bar = 1;\n return \$bar;\n}", $method->toString());
188188
}
189+
190+
public function testExceptionMessageOnName()
191+
{
192+
try {
193+
new PhpMethod(0);
194+
} catch (\InvalidArgumentException $e) {
195+
$this->assertSame('Name "0" is invalid when instantiating PhpMethod object', $e->getMessage());
196+
}
197+
}
189198
}

0 commit comments

Comments
 (0)