| Conditions | 3 |
| Paths | 3 |
| Total Lines | 25 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 17 |
| CRAP Score | 3 |
| Changes | 0 | ||
| 1 | <?php |
||
| 68 | 7 | public function toString() { |
|
| 69 | 7 | if ( empty( $this->getName() ) ) { |
|
| 70 | 1 | throw new Exception( 'View must have a name.' ); |
|
| 71 | } |
||
| 72 | |||
| 73 | 6 | if ( empty( $this->getFilepath() ) ) { |
|
| 74 | 1 | throw new Exception( 'View must have a filepath.' ); |
|
| 75 | } |
||
| 76 | |||
| 77 | 5 | $global_context = ['global' => ViewService::getGlobals()]; |
|
| 78 | 5 | $local_context = $this->getContext(); |
|
| 79 | |||
| 80 | 5 | $this->with( $global_context ); |
|
| 81 | 5 | ViewService::compose( $this ); |
|
| 82 | 5 | $this->with( $local_context ); |
|
| 83 | |||
| 84 | 5 | $renderer = function() { |
|
| 85 | 5 | ob_start(); |
|
| 86 | 5 | extract( $this->getContext() ); |
|
| 87 | 5 | include( $this->getFilepath() ); |
|
| 88 | 5 | return ob_get_clean(); |
|
| 89 | 5 | }; |
|
| 90 | |||
| 91 | 5 | return $renderer(); |
|
| 92 | } |
||
| 93 | |||
| 103 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: