Passed
Push — master ( a8bd8b...f2f9a5 )
by Atanas
02:01
created

PhpViewEngine::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace WPEmerge\View;
4
5
use Exception;
6
use View as ViewService;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, WPEmerge\View\ViewService.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
8
/**
9
 * Render view files with php.
10
 */
11
class PhpViewEngine implements ViewEngineInterface {
12
	/**
13
	 * {@inheritDoc}
14
	 */
15 1
	public function exists( $view ) {
16 1
		$file = $this->resolveFilepath( $view );
17 1
		return strlen( $file ) > 0;
18
	}
19
20
	/**
21
	 * {@inheritDoc}
22
	 */
23 3
	public function canonical( $view ) {
24 3
		$file = $this->resolveFilepath( $view );
25 3
		return $file;
26
	}
27
28
	/**
29
	 * {@inheritDoc}
30
	 */
31 6
	public function make( $views, $context = [] ) {
32 6
		foreach ( $views as $view ) {
33 6
			if ( $this->exists( $view ) ) {
34 5
				$filepath = $this->resolveFilepath( $view );
35 5
				return $this->makeView( $view, $filepath, $context );
36
			}
37 1
		}
38
39 1
		throw new Exception( 'View not found for "' . implode( ', ', $views ) . '"' );
40
	}
41
42
	/**
43
	 * Create a view instance.
44
	 *
45
	 * @param  string $name
46
	 * @param  string $filepath
47
	 * @param  array  $context
48
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be PhpView?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
49
	 */
50 5
	protected function makeView( $name, $filepath, $context = [] ) {
51 5
		return (new PhpView())
52 5
			->setName( $name )
53 5
			->setFilepath( $filepath )
54 5
			->with( $context );
55
	}
56
57
	/**
58
	 * Resolve a view or a view array to an absolute filepath
59
	 *
60
	 * @param  string $view
61
	 * @return string
62
	 */
63 3
	protected function resolveFilepath( $view ) {
64 3
		$file = locate_template( $view, false );
65
66 3
		if ( ! $file ) {
67
			// locate_template failed to find the view - try adding a .php extension
68 3
			$file = locate_template( $view . '.php', false );
69 3
		}
70
71 3
		if ( ! $file ) {
72
			// locate_template failed to find the view - test if a valid absolute path was passed
73 3
			$file = $this->resolveFilepathFromFilesystem( $view );
74 3
		}
75
76 3
		if ( $file ) {
77 1
			$file = realpath( $file );
78 1
		}
79
80 3
		return $file;
81
	}
82
83
	/**
84
	 * Resolve the view if it exists on the filesystem
85
	 *
86
	 * @param  string $view
87
	 * @return string
88
	 */
89 3
	protected function resolveFilepathFromFilesystem( $view ) {
90 3
		return file_exists( $view ) ? $view : '';
91
	}
92
}
93