Completed
Push — master ( a4ba22...5297ca )
by Glynn
09:37 queued 09:34
created
src/Services/View/PHP_Engine.php 1 patch
Spacing   +44 added lines, -44 removed lines patch added patch discarded remove patch
@@ -53,8 +53,8 @@  discard block
 block discarded – undo
53 53
 	 *
54 54
 	 * @param string $base_view_path
55 55
 	 */
56
-	public function __construct( string $base_view_path ) {
57
-		$this->base_view_path = $this->verify_view_path( $base_view_path );
56
+	public function __construct(string $base_view_path) {
57
+		$this->base_view_path = $this->verify_view_path($base_view_path);
58 58
 	}
59 59
 
60 60
 	/**
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
 	 * @param Component_Compiler $compiler
64 64
 	 * @return void
65 65
 	 */
66
-	public function set_component_compiler( Component_Compiler $compiler ): void {
66
+	public function set_component_compiler(Component_Compiler $compiler): void {
67 67
 		$this->component_compiler = $compiler;
68 68
 	}
69 69
 
@@ -75,13 +75,13 @@  discard block
 block discarded – undo
75 75
 	 * @param boolean                 $print
76 76
 	 * @return string|null
77 77
 	 */
78
-	public function render( string $view, iterable $data, bool $print = true ): ?string {
79
-		$view = $this->resolve_file_path( $view );
80
-		if ( $print ) {
81
-			print( $this->render_buffer( $view, $data ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
78
+	public function render(string $view, iterable $data, bool $print = true): ?string {
79
+		$view = $this->resolve_file_path($view);
80
+		if ($print) {
81
+			print($this->render_buffer($view, $data)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
82 82
 			return null;
83 83
 		} else {
84
-			return $this->render_buffer( $view, $data );
84
+			return $this->render_buffer($view, $data);
85 85
 		}
86 86
 	}
87 87
 
@@ -91,22 +91,22 @@  discard block
 block discarded – undo
91 91
 	 * @param Component $component
92 92
 	 * @return string
93 93
 	 */
94
-	public function component( Component $component, bool $print = true ): ?string {
94
+	public function component(Component $component, bool $print = true): ?string {
95 95
 
96 96
 		// Throw exception of no compiler passed.
97
-		if ( ! Object_Helper::is_a( $this->component_compiler, Component_Compiler::class ) ) {
98
-			throw new Exception( 'No component compiler passed to PHP_Engine' );
97
+		if ( ! Object_Helper::is_a($this->component_compiler, Component_Compiler::class)) {
98
+			throw new Exception('No component compiler passed to PHP_Engine');
99 99
 		}
100 100
 
101 101
 		// Compile the component.
102
-		$compiled = $this->component_compiler->compile( $component ); // @phpstan-ignore-line, checked above.
103
-		$template = $this->maybe_resolve_dot_notation( $compiled->template() );
104
-		$view     = sprintf( '%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim( $template ) );
105
-		if ( $print ) {
106
-			print( $this->render_buffer( $view, $compiled->data() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
102
+		$compiled = $this->component_compiler->compile($component); // @phpstan-ignore-line, checked above.
103
+		$template = $this->maybe_resolve_dot_notation($compiled->template());
104
+		$view     = sprintf('%s%s%s.php', $this->base_view_path, \DIRECTORY_SEPARATOR, trim($template));
105
+		if ($print) {
106
+			print($this->render_buffer($view, $compiled->data())); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
107 107
 			return null;
108 108
 		} else {
109
-			return $this->render_buffer( $view, $compiled->data() );
109
+			return $this->render_buffer($view, $compiled->data());
110 110
 		}
111 111
 	}
112 112
 
@@ -117,8 +117,8 @@  discard block
 block discarded – undo
117 117
 	 * @param View_Model $view_model
118 118
 	 * @return string|null
119 119
 	 */
120
-	public function view_model( View_Model $view_model, bool $print = true ): ?string {
121
-		return $this->render( $view_model->template(), $view_model->data(), $print );
120
+	public function view_model(View_Model $view_model, bool $print = true): ?string {
121
+		return $this->render($view_model->template(), $view_model->data(), $print);
122 122
 	}
123 123
 
124 124
 	/**
@@ -129,12 +129,12 @@  discard block
 block discarded – undo
129 129
 	 * @param boolean                 $print
130 130
 	 * @return string|null
131 131
 	 */
132
-	public function partial( string $view, iterable $data = array(), bool $print = true ): ?string {
133
-		if ( $print ) {
134
-			$this->render( $view, $data, $print );
132
+	public function partial(string $view, iterable $data = array(), bool $print = true): ?string {
133
+		if ($print) {
134
+			$this->render($view, $data, $print);
135 135
 			return null;
136 136
 		} else {
137
-			return $this->render( $view, $data, $print );
137
+			return $this->render($view, $data, $print);
138 138
 		}
139 139
 	}
140 140
 
@@ -146,27 +146,27 @@  discard block
 block discarded – undo
146 146
 	 * @return string
147 147
 	 * @throws Exception
148 148
 	 */
149
-	private function render_buffer( string $view, iterable $__data ): string {
149
+	private function render_buffer(string $view, iterable $__data): string {
150 150
 
151
-		if ( ! file_exists( $view ) ) {
152
-			throw new Exception( "{$view} doesn't exist" );
151
+		if ( ! file_exists($view)) {
152
+			throw new Exception("{$view} doesn't exist");
153 153
 		}
154 154
 
155 155
 		$output = '';
156 156
 		ob_start();
157 157
 
158 158
 		// Set all the data values a parameters.
159
-		foreach ( $__data as $__key => $__value ) {
160
-			if ( is_string( $__key ) ) {
161
-				${\wp_strip_all_tags( $__key )} = $__value;
159
+		foreach ($__data as $__key => $__value) {
160
+			if (is_string($__key)) {
161
+				${\wp_strip_all_tags($__key)} = $__value;
162 162
 			}
163 163
 
164 164
 			// Unset the key and value.
165
-			unset( $__key, $__value );
165
+			unset($__key, $__value);
166 166
 		}
167 167
 
168 168
 		// Unset the data.
169
-		unset( $__data );
169
+		unset($__data);
170 170
 
171 171
 		include $view;
172 172
 		$output = ob_get_contents();
@@ -180,12 +180,12 @@  discard block
 block discarded – undo
180 180
 	 * @param string $filename
181 181
 	 * @return string
182 182
 	 */
183
-	private function resolve_file_path( string $filename ): string {
184
-		$filename = $this->maybe_resolve_dot_notation( $filename );
183
+	private function resolve_file_path(string $filename): string {
184
+		$filename = $this->maybe_resolve_dot_notation($filename);
185 185
 		return sprintf(
186 186
 			'%s%s.php',
187 187
 			$this->base_view_path,
188
-			trim( $filename )
188
+			trim($filename)
189 189
 		);
190 190
 	}
191 191
 
@@ -195,13 +195,13 @@  discard block
 block discarded – undo
195 195
 	 * @param string $filename
196 196
 	 * @return string
197 197
 	 */
198
-	private function maybe_resolve_dot_notation( string $filename ): string {
199
-		if ( endsWith( '.php' )( $filename ) ) {
200
-			$filename = substr( $filename, 0, -4 );
198
+	private function maybe_resolve_dot_notation(string $filename): string {
199
+		if (endsWith('.php')($filename)) {
200
+			$filename = substr($filename, 0, -4);
201 201
 		}
202 202
 
203
-		$parts    = explode( '.', $filename );
204
-		$filename = implode( DIRECTORY_SEPARATOR, $parts );
203
+		$parts    = explode('.', $filename);
204
+		$filename = implode(DIRECTORY_SEPARATOR, $parts);
205 205
 
206 206
 		return $filename;
207 207
 	}
@@ -213,12 +213,12 @@  discard block
 block discarded – undo
213 213
 	 * @return string
214 214
 	 * @throws Exception
215 215
 	 */
216
-	private function verify_view_path( string $path ): string {
217
-		$path = $this->maybe_resolve_dot_notation( $path );
218
-		$path = rtrim( $path, '/' ) . '/';
216
+	private function verify_view_path(string $path): string {
217
+		$path = $this->maybe_resolve_dot_notation($path);
218
+		$path = rtrim($path, '/') . '/';
219 219
 
220
-		if ( ! \is_dir( $path ) ) {
221
-			throw new Exception( "{$path} doesn't exist and cant be used as the base view path." );
220
+		if ( ! \is_dir($path)) {
221
+			throw new Exception("{$path} doesn't exist and cant be used as the base view path.");
222 222
 		}
223 223
 
224 224
 		return $path;
Please login to merge, or discard this patch.