Skip to content

Commit 7cd83e8

Browse files
committed
PhpGenerator: added Helpers::isIdentifier() and Method::setBody()
1 parent 9368e43 commit 7cd83e8

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/PhpGenerator/Helpers.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,23 @@ public static function formatArgs($statement, array $args)
175175
*/
176176
public static function formatMember($name)
177177
{
178-
return $name instanceof PhpLiteral || !preg_match('#^' . self::PHP_IDENT . '$#', $name)
178+
return $name instanceof PhpLiteral || !self::isIdentifier($name)
179179
? '{' . self::_dump($name) . '}'
180180
: $name ;
181181
}
182182

183183

184184

185+
/**
186+
* @return bool
187+
*/
188+
public static function isIdentifier($value)
189+
{
190+
return is_string($value) && preg_match('#^' . self::PHP_IDENT . '$#', $value);
191+
}
192+
193+
194+
185195
public static function createObject($class, array $props)
186196
{
187197
return unserialize('O' . substr(serialize((string) $class), 1, -1) . substr(serialize($props), 1));

src/PhpGenerator/Method.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public function addUse($name)
8383

8484

8585

86+
/** @return Method */
87+
public function setBody($statement, array $args = NULL)
88+
{
89+
$this->body = func_num_args() > 1 ? Helpers::formatArgs($statement, $args) : $statement;
90+
return $this;
91+
}
92+
93+
94+
8695
/** @return Method */
8796
public function addBody($statement, array $args = NULL)
8897
{

tests/PhpGenerator/PhpGenerator.class.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $class->addMethod('getHandle')
4646
->addDocument('Returns file handle.')
4747
->addDocument('@return resource')
4848
->setFinal(TRUE)
49-
->setBody('return $this->handle;');
49+
->setBody('return $this->?;', array('handle'));
5050

5151
$class->addMethod('getSections')
5252
->setStatic(TRUE)

tests/PhpGenerator/PhpGenerator.format.phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ Assert::same( '$obj->{\' \'} = 2', Helpers::formatArgs('$obj->? = ?', array(' ',
3838

3939
Assert::same( "Item", Helpers::formatMember('Item') );
4040
Assert::same( "{'0Item'}", Helpers::formatMember('0Item') );
41+
42+
Assert::true( Helpers::isIdentifier('Item') );
43+
Assert::false( Helpers::isIdentifier('0Item') );

0 commit comments

Comments
 (0)