diff --git a/.php_cs b/.php_cs index 1559c2c..271c6a1 100755 --- a/.php_cs +++ b/.php_cs @@ -1,10 +1,18 @@ exclude('vendor') ->exclude('tests') ->in(__DIR__); -return Symfony\CS\Config\Config::create() - ->level(Symfony\CS\FixerInterface::PSR2_LEVEL) - ->finder($finder); \ No newline at end of file +return PhpCsFixer\Config::create() + ->setUsingCache(false) + ->setRules(array( + '@PSR2' => true, + 'binary_operator_spaces' => true, + 'no_whitespace_in_blank_line' => true, + 'ternary_operator_spaces' => true, + 'cast_spaces' => true, + 'trailing_comma_in_multiline_array' => true + )) + ->setFinder($finder); diff --git a/.travis.yml b/.travis.yml index e8c230f..a39ad16 100755 --- a/.travis.yml +++ b/.travis.yml @@ -7,16 +7,17 @@ php: - 5.4 - 5.5 - 5.6 - - 7 + - 7.0 + - 7.1 before_install: - composer self-update install: - - composer install --no-dev + - composer install script: - - phpunit --coverage-text --coverage-clover=coverage.clover + - ./vendor/phpunit/phpunit/phpunit --coverage-text --coverage-clover=coverage.clover after_script: - wget https://scrutinizer-ci.com/ocular.phar diff --git a/CHANGELOG.md b/CHANGELOG.md index 7753514..cd05d58 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## 1.2.0 +- issue #7 - Improve Exception message + ## 1.1.0 - issue #5 - No autocomplete because of return type on a new line after @return in annotation diff --git a/composer.json b/composer.json index b00cd09..ac600ec 100755 --- a/composer.json +++ b/composer.json @@ -1,30 +1,29 @@ { - "name": "wsdltophp/phpgenerator", - "description": "Generate php source file", - "type": "library", - "keywords" : ["php","generator"], - "homepage" : "https://github.com/WsdlToPhp/PhpGenerator", - "license" : "MIT", - "authors" : [ - { - "name": "Mikaël DELSOL", - "email": "contact@wsdltophp.com", - "role": "Owner" - } - ], - "support" : { - "email" : "contact@wsdltophp.com" - }, - "require": { + "name" : "wsdltophp/phpgenerator", + "description" : "Generate php source file", + "require" : { "php" : ">=5.3.3" }, "require-dev": { - "fabpot/php-cs-fixer": "~1.8" + "friendsofphp/php-cs-fixer": "~2.0", + "phpunit/phpunit": "~4.0" }, - "autoload": { - "psr-4": { - "WsdlToPhp\\PhpGenerator\\": "src", - "WsdlToPhp\\PhpGenerator\\Tests\\": "tests" + "license" : "MIT", + "keywords" : [ "php", "generator" ], + "autoload" : { + "psr-4" : { + "WsdlToPhp\\PhpGenerator\\" : "src", + "WsdlToPhp\\PhpGenerator\\Tests\\" : "tests" } - } + }, + "type" : "library", + "support" : { + "email" : "contact@wsdltophp.com" + }, + "homepage" : "https://github.com/WsdlToPhp/PhpGenerator", + "authors" : [ { + "name" : "Mikaël DELSOL", + "email" : "contact@wsdltophp.com", + "role" : "Owner" + } ] } diff --git a/src/Element/AbstractAssignedValueElement.php b/src/Element/AbstractAssignedValueElement.php index 58ded0c..a6f7b7e 100755 --- a/src/Element/AbstractAssignedValueElement.php +++ b/src/Element/AbstractAssignedValueElement.php @@ -31,7 +31,7 @@ public function __construct($name, $value = null, $access = parent::ACCESS_PUBLI public function setValue($value) { if ($this->getAcceptNonScalarValue() === false && !is_scalar($value) && $value !== null) { - throw new \InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value', gettype($value))); + throw new \InvalidArgumentException(sprintf('Value of type "%s" is not a valid scalar value for %s object', gettype($value), $this->getCalledClass())); } $this->value = $value; return $this; diff --git a/src/Element/AbstractElement.php b/src/Element/AbstractElement.php index 1c96b01..e4d7c85 100755 --- a/src/Element/AbstractElement.php +++ b/src/Element/AbstractElement.php @@ -32,7 +32,7 @@ public function __construct($name) public function setName($name) { if (!self::nameIsValid($name)) { - throw new \InvalidArgumentException(sprintf('Name "%s" is invalid, please provide a valid name', $name)); + throw new \InvalidArgumentException(sprintf('Name "%s" is invalid when instantiating %s object', $name, $this->getCalledClass())); } $this->name = $name; return $this; @@ -196,7 +196,7 @@ protected function childIsValid($child) $valid |= (gettype($child) === $authorizedType) || self::objectIsValid($child, $authorizedType); } } - return (bool)$valid; + return (bool) $valid; } /** * @return AbstractElement[]|mixed[] @@ -324,9 +324,16 @@ public function getIndentationString($indentation = null) public function getIndentedString($string, $indentation = null) { $strings = explode(self::BREAK_LINE_CHAR, $string); - foreach ($strings as $i=>$s) { + foreach ($strings as $i => $s) { $strings[$i] = sprintf('%s%s', $this->getIndentationString($indentation), $s); } return implode(self::BREAK_LINE_CHAR, $strings); } + /** + * @return string + */ + final public function getCalledClass() + { + return substr(get_called_class(), strrpos(get_called_class(), '\\') + 1); + } } diff --git a/src/Element/PhpAnnotationBlock.php b/src/Element/PhpAnnotationBlock.php index 5d98675..c4c39fe 100755 --- a/src/Element/PhpAnnotationBlock.php +++ b/src/Element/PhpAnnotationBlock.php @@ -64,7 +64,7 @@ protected static function annotationsAreValid(array $annotations) foreach ($annotations as $annotation) { $valid &= self::annotationIsValid($annotation); } - return (bool)$valid; + return (bool) $valid; } /** * @param string|array|PhpAnnotation $annotation diff --git a/src/Element/PhpClass.php b/src/Element/PhpClass.php index 0f73b9e..73a588c 100755 --- a/src/Element/PhpClass.php +++ b/src/Element/PhpClass.php @@ -131,7 +131,7 @@ public static function interfacesAreValid(array $interfaces = array()) foreach ($interfaces as $interface) { $valid &= self::interfaceIsValid($interface); } - return (bool)$valid; + return (bool) $valid; } /** * @param string|PhpClass $interface diff --git a/src/Element/PhpFunction.php b/src/Element/PhpFunction.php index 93eaa1c..f50f4a7 100755 --- a/src/Element/PhpFunction.php +++ b/src/Element/PhpFunction.php @@ -65,7 +65,7 @@ public static function parametersAreValid(array $parameters) foreach ($parameters as $parameter) { $valid &= self::parameterIsValid($parameter); } - return (bool)$valid; + return (bool) $valid; } /** * @param string|array|PhpFunctionParameter $parameter diff --git a/tests/Element/PhpAnnotationTest.php b/tests/Element/PhpAnnotationTest.php index c83ff3d..6612212 100755 --- a/tests/Element/PhpAnnotationTest.php +++ b/tests/Element/PhpAnnotationTest.php @@ -68,4 +68,13 @@ public function testHasContent() $this->assertTrue($annotation->hasContent()); } + + public function testExceptionMessageOnName() + { + try { + new PhpAnnotation(0, ''); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpAnnotation object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpClassTest.php b/tests/Element/PhpClassTest.php index 2eaa297..8aa827b 100755 --- a/tests/Element/PhpClassTest.php +++ b/tests/Element/PhpClassTest.php @@ -255,4 +255,13 @@ public function testExtendsFromNamespace() $this->assertSame("class Foo extends \\DOMDocument\n{\n}", $class->toString()); } + + public function testExceptionMessageOnName() + { + try { + new PhpClass(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpClass object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpConstantTest.php b/tests/Element/PhpConstantTest.php index f410808..9f3662d 100755 --- a/tests/Element/PhpConstantTest.php +++ b/tests/Element/PhpConstantTest.php @@ -131,4 +131,22 @@ public function testGetChildrenTypes() $this->assertSame(array(), $constant->getChildrenTypes()); } + + public function testExceptionMessageOnName() + { + try { + new PhpConstant(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpConstant object', $e->getMessage()); + } + } + + public function testExceptionMessageOnValue() + { + try { + new PhpConstant('Foo', new \stdClass()); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Value of type "object" is not a valid scalar value for PhpConstant object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpFileTest.php b/tests/Element/PhpFileTest.php index 9f392e4..a5631cd 100755 --- a/tests/Element/PhpFileTest.php +++ b/tests/Element/PhpFileTest.php @@ -81,4 +81,13 @@ public function testAnnotationClassMethodBlockToString() $this->assertSame("toString()); } + + public function testExceptionMessageOnName() + { + try { + new PhpFile(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpFile object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpFunctionParameterTest.php b/tests/Element/PhpFunctionParameterTest.php index 75b88f6..faed5ba 100755 --- a/tests/Element/PhpFunctionParameterTest.php +++ b/tests/Element/PhpFunctionParameterTest.php @@ -55,4 +55,13 @@ public function testToStringWithNamespace() $this->assertSame('My\Name\Space $foo = null', $functionParameter->toString()); } + + public function testExceptionMessageOnName() + { + try { + new PhpFunctionParameter(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpFunctionParameter object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpFunctionTest.php b/tests/Element/PhpFunctionTest.php index 3bb6a6a..5fe8154 100755 --- a/tests/Element/PhpFunctionTest.php +++ b/tests/Element/PhpFunctionTest.php @@ -114,4 +114,13 @@ public function testToStringWithBody() $this->assertSame("function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true)\n{\n \$bar = 1;\n return \$bar;\n}", $function->toString()); } + + public function testExceptionMessageOnName() + { + try { + new PhpFunction(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpFunction object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpInterfaceTest.php b/tests/Element/PhpInterfaceTest.php index 4db778f..dd6526d 100755 --- a/tests/Element/PhpInterfaceTest.php +++ b/tests/Element/PhpInterfaceTest.php @@ -97,4 +97,13 @@ public function testSimpleIntefaceEmptyPublicMethodToString() $this->assertSame("interface Foo\n{\n public function bar();\n}", $interface->toString()); } + + public function testExceptionMessageOnName() + { + try { + new PhpInterface(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpInterface object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpMethodTest.php b/tests/Element/PhpMethodTest.php index 253243a..2aa17d5 100755 --- a/tests/Element/PhpMethodTest.php +++ b/tests/Element/PhpMethodTest.php @@ -186,4 +186,13 @@ public function testPublicWithBodyToString() $this->assertSame("public function foo(\$bar, \$demo = 1, \$sample = null, \$deamon = true)\n{\n \$bar = 1;\n return \$bar;\n}", $method->toString()); } + + public function testExceptionMessageOnName() + { + try { + new PhpMethod(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpMethod object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpPropertyTest.php b/tests/Element/PhpPropertyTest.php index 3fc765c..cd9d63d 100755 --- a/tests/Element/PhpPropertyTest.php +++ b/tests/Element/PhpPropertyTest.php @@ -100,4 +100,13 @@ public function testSetAccess() $property->setAccess(' public'); } + + public function testExceptionMessageOnName() + { + try { + new PhpProperty(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpProperty object', $e->getMessage()); + } + } } diff --git a/tests/Element/PhpVariableTest.php b/tests/Element/PhpVariableTest.php index 273abcc..cfcc82f 100755 --- a/tests/Element/PhpVariableTest.php +++ b/tests/Element/PhpVariableTest.php @@ -123,4 +123,13 @@ public function testé() $é = 4; $this->assertEquals(4, $é); } + + public function testExceptionMessageOnName() + { + try { + new PhpVariable(0); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Name "0" is invalid when instantiating PhpVariable object', $e->getMessage()); + } + } }