-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing class not in root namespace in generated code
- Loading branch information
Showing
12 changed files
with
108 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
|
||
namespace Safe; | ||
|
||
class Type | ||
{ | ||
/** | ||
* Returns true if the type passed in parameter is a class, false if it is scalar or resource | ||
* | ||
* @param string $type | ||
* @return bool | ||
*/ | ||
private static function isClass(string $type): bool | ||
{ | ||
if ($type === '') { | ||
throw new \RuntimeException('Empty type passed'); | ||
} | ||
if ($type === 'stdClass') { | ||
return true; | ||
} | ||
// Classes start with uppercase letters. Otherwise, it's most likely a scalar. | ||
if ($type[0] === strtoupper($type[0])) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Put classes in the root namespace | ||
* | ||
* @param string $type | ||
* @return string | ||
*/ | ||
public static function toRootNamespace(string $type): string | ||
{ | ||
if (self::isClass($type)) { | ||
return '\\'.$type; | ||
} | ||
return $type; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Safe; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class TypeTest extends TestCase | ||
{ | ||
public function testIsClass() | ||
{ | ||
$this->assertSame('\\stdClass', Type::toRootNamespace('stdClass')); | ||
$this->assertSame('\\SimpleXMLElement', Type::toRootNamespace('SimpleXMLElement')); | ||
$this->assertSame('bool', Type::toRootNamespace('bool')); | ||
$this->assertSame('int', Type::toRootNamespace('int')); | ||
} | ||
} |