浏览代码

Use traits

mscherer 5 年之前
父节点
当前提交
87ea3255fc
共有 5 个文件被更改,包括 195 次插入171 次删除
  1. 1 80
      src/View/Helper/HtmlHelper.php
  2. 88 0
      src/View/Helper/HtmlTrait.php
  3. 1 91
      src/View/Helper/UrlHelper.php
  4. 99 0
      src/View/Helper/UrlTrait.php
  5. 6 0
      tests/phpstan.neon

+ 1 - 80
src/View/Helper/HtmlHelper.php

@@ -24,85 +24,6 @@ use Cake\View\Helper\HtmlHelper as CoreHtmlHelper;
  */
 class HtmlHelper extends CoreHtmlHelper {
 
-	/**
-	 * Display image tag from blob content.
-	 * Enhancement for HtmlHelper. Defaults to png image
-	 *
-	 * Options:
-	 * - type: png, gif, jpg, ...
-	 *
-	 * @param string $content Data in binary form
-	 * @param array $options Attributes
-	 * @return string HTML image tag
-	 */
-	public function imageFromBlob($content, array $options = []) {
-		$options += ['type' => 'png'];
-		$mimeType = 'image/' . $options['type'];
-
-		$text = 'data:' . $mimeType . ';base64,' . base64_encode($content);
-
-		return $this->formatTemplate('image', [
-			'url' => $text,
-			'attrs' => $this->templater()->formatAttributes($options, ['block', 'link']),
-		]);
-	}
-
-	/**
-	 * Creates a reset HTML link.
-	 * The prefix and plugin params are resetting to default false.
-	 *
-	 * ### Options
-	 *
-	 * - `escape` Set to false to disable escaping of title and attributes.
-	 * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
-	 *   over value of `escape`)
-	 * - `confirm` JavaScript confirmation message.
-	 *
-	 * @param string $title The content to be wrapped by <a> tags.
-	 * @param string|array|null $url URL or array of URL parameters, or
-	 *   external URL (starts with http://)
-	 * @param array $options Array of options and HTML attributes.
-	 * @return string An `<a />` element.
-	 */
-	public function linkReset($title, $url = null, array $options = []) {
-		if (is_array($url)) {
-			$url += ['prefix' => false, 'plugin' => false];
-		}
-
-		return parent::link($title, $url, $options);
-	}
-
-	/**
-	 * Keep query string params for pagination/filter for this link,
-	 * e.g. after edit action.
-	 *
-	 * ### Options
-	 *
-	 * - `escape` Set to false to disable escaping of title and attributes.
-	 * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
-	 *   over value of `escape`)
-	 * - `confirm` JavaScript confirmation message.
-	 *
-	 * @param string $title The content to be wrapped by <a> tags.
-	 * @param string|array|null $url URL or array of URL parameters, or
-	 *   external URL (starts with http://)
-	 * @param array $options Array of options and HTML attributes.
-	 * @return string An `<a />` element.
-	 * @return string Link
-	 */
-	public function linkComplete($title, $url = null, array $options = []) {
-		if (is_array($url)) {
-			// Add query strings
-			if (!isset($url['?'])) {
-				$url['?'] = [];
-			}
-			$url['?'] += $this->_View->getRequest()->getQuery();
-
-			$pass = $this->_View->getRequest()->getParam('pass');
-			$url = array_merge($url, $pass);
-		}
-
-		return parent::link($title, $url, $options);
-	}
+	use HtmlTrait;
 
 }

+ 88 - 0
src/View/Helper/HtmlTrait.php

@@ -0,0 +1,88 @@
+<?php
+
+namespace Tools\View\Helper;
+
+trait HtmlTrait {
+
+	/**
+	 * Display image tag from blob content.
+	 * Enhancement for HtmlHelper. Defaults to png image
+	 *
+	 * Options:
+	 * - type: png, gif, jpg, ...
+	 *
+	 * @param string $content Data in binary form
+	 * @param array $options Attributes
+	 * @return string HTML image tag
+	 */
+	public function imageFromBlob($content, array $options = []) {
+		$options += ['type' => 'png'];
+		$mimeType = 'image/' . $options['type'];
+
+		$text = 'data:' . $mimeType . ';base64,' . base64_encode($content);
+
+		return $this->formatTemplate('image', [
+			'url' => $text,
+			'attrs' => $this->templater()->formatAttributes($options, ['block', 'link']),
+		]);
+	}
+
+	/**
+	 * Creates a reset HTML link.
+	 * The prefix and plugin params are resetting to default false.
+	 *
+	 * ### Options
+	 *
+	 * - `escape` Set to false to disable escaping of title and attributes.
+	 * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
+	 *   over value of `escape`)
+	 * - `confirm` JavaScript confirmation message.
+	 *
+	 * @param string $title The content to be wrapped by <a> tags.
+	 * @param string|array|null $url URL or array of URL parameters, or
+	 *   external URL (starts with http://)
+	 * @param array $options Array of options and HTML attributes.
+	 * @return string An `<a />` element.
+	 */
+	public function linkReset($title, $url = null, array $options = []) {
+		if (is_array($url)) {
+			$url += ['prefix' => false, 'plugin' => false];
+		}
+
+		return parent::link($title, $url, $options);
+	}
+
+	/**
+	 * Keep query string params for pagination/filter for this link,
+	 * e.g. after edit action.
+	 *
+	 * ### Options
+	 *
+	 * - `escape` Set to false to disable escaping of title and attributes.
+	 * - `escapeTitle` Set to false to disable escaping of title. Takes precedence
+	 *   over value of `escape`)
+	 * - `confirm` JavaScript confirmation message.
+	 *
+	 * @param string $title The content to be wrapped by <a> tags.
+	 * @param string|array|null $url URL or array of URL parameters, or
+	 *   external URL (starts with http://)
+	 * @param array $options Array of options and HTML attributes.
+	 * @return string An `<a />` element.
+	 * @return string Link
+	 */
+	public function linkComplete($title, $url = null, array $options = []) {
+		if (is_array($url)) {
+			// Add query strings
+			if (!isset($url['?'])) {
+				$url['?'] = [];
+			}
+			$url['?'] += $this->_View->getRequest()->getQuery();
+
+			$pass = $this->_View->getRequest()->getParam('pass');
+			$url = array_merge($url, $pass);
+		}
+
+		return parent::link($title, $url, $options);
+	}
+
+}

+ 1 - 91
src/View/Helper/UrlHelper.php

@@ -25,96 +25,6 @@ use Cake\View\Helper\UrlHelper as CoreUrlHelper;
  */
 class UrlHelper extends CoreUrlHelper {
 
-	/**
-	 * @param array $url
-	 * @return array
-	 */
-	public function resetArray(array $url): array {
-		$url += $this->defaults();
-
-		return $url;
-	}
-
-	/**
-	 * @param array $url
-	 * @return array
-	 */
-	public function completeArray(array $url): array {
-		$url = $this->addQueryStrings($url);
-		$url = $this->addPassed($url);
-
-		return $url;
-	}
-
-	/**
-	 * Creates a reset URL.
-	 * The prefix and plugin params are resetting to default false.
-	 *
-	 * Can only add defaults for array URLs.
-	 *
-	 * @param string|array|null $url URL.
-	 * @param array $options
-	 * @return string Full translated URL with base path.
-	 */
-	public function buildReset($url, array $options = []): string {
-		if (is_array($url)) {
-			$url += $this->defaults();
-		}
-
-		return $this->build($url, $options);
-	}
-
-	/**
-	 * Returns a URL based on provided parameters.
-	 *
-	 * Can only add query strings for array URLs.
-	 *
-	 * @param string|array|null $url URL.
-	 * @param array $options
-	 * @return string Full translated URL with base path.
-	 */
-	public function buildComplete($url, array $options = []): string {
-		if (is_array($url)) {
-			$url = $this->addQueryStrings($url);
-		}
-
-		return $this->build($url, $options);
-	}
-
-	/**
-	 * @return array
-	 */
-	public function defaults(): array {
-		return [
-			'prefix' => false,
-			'plugin' => false,
-		];
-	}
-
-	/**
-	 * @param array $url
-	 *
-	 * @return array
-	 */
-	protected function addQueryStrings(array $url): array {
-		if (!isset($url['?'])) {
-			$url['?'] = [];
-		}
-		$url['?'] += $this->_View->getRequest()->getQuery();
-
-		return $url;
-	}
-
-	/**
-	 * @param array $url
-	 *
-	 * @return array
-	 */
-	protected function addPassed(array $url) {
-		$pass = $this->_View->getRequest()->getParam('pass');
-		$url = array_merge($url, $pass);
-
-		return $url;
-	}
+	use UrlTrait;
 
 }

+ 99 - 0
src/View/Helper/UrlTrait.php

@@ -0,0 +1,99 @@
+<?php
+
+namespace Tools\View\Helper;
+
+trait UrlTrait {
+
+	/**
+	 * @param array $url
+	 * @return array
+	 */
+	public function resetArray(array $url): array {
+		$url += $this->defaults();
+
+		return $url;
+	}
+
+	/**
+	 * @param array $url
+	 * @return array
+	 */
+	public function completeArray(array $url): array {
+		$url = $this->addQueryStrings($url);
+		$url = $this->addPassed($url);
+
+		return $url;
+	}
+
+	/**
+	 * Creates a reset URL.
+	 * The prefix and plugin params are resetting to default false.
+	 *
+	 * Can only add defaults for array URLs.
+	 *
+	 * @param string|array|null $url URL.
+	 * @param array $options
+	 * @return string Full translated URL with base path.
+	 */
+	public function buildReset($url, array $options = []): string {
+		if (is_array($url)) {
+			$url += $this->defaults();
+		}
+
+		return $this->build($url, $options);
+	}
+
+	/**
+	 * Returns a URL based on provided parameters.
+	 *
+	 * Can only add query strings for array URLs.
+	 *
+	 * @param string|array|null $url URL.
+	 * @param array $options
+	 * @return string Full translated URL with base path.
+	 */
+	public function buildComplete($url, array $options = []): string {
+		if (is_array($url)) {
+			$url = $this->addQueryStrings($url);
+		}
+
+		return $this->build($url, $options);
+	}
+
+	/**
+	 * @return array
+	 */
+	public function defaults(): array {
+		return [
+			'prefix' => false,
+			'plugin' => false,
+		];
+	}
+
+	/**
+	 * @param array $url
+	 *
+	 * @return array
+	 */
+	protected function addQueryStrings(array $url): array {
+		if (!isset($url['?'])) {
+			$url['?'] = [];
+		}
+		$url['?'] += $this->_View->getRequest()->getQuery();
+
+		return $url;
+	}
+
+	/**
+	 * @param array $url
+	 *
+	 * @return array
+	 */
+	protected function addPassed(array $url) {
+		$pass = $this->_View->getRequest()->getParam('pass');
+		$url = array_merge($url, $pass);
+
+		return $url;
+	}
+
+}

+ 6 - 0
tests/phpstan.neon

@@ -14,6 +14,12 @@ parameters:
 			message: '#Variable \$.+ might not be defined.#'
 			path: '%rootDir%/../../../src/View/Helper/TreeHelper.php'
 		-
+			message: '#Negated boolean expression is always true.#'
+			path: '%rootDir%/../../../src/View/Helper/TreeHelper.php'
+		-
+			message: '#Result of \|\| is always true.#'
+			path: '%rootDir%/../../../src/View/Helper/TreeHelper.php'
+		-
 			message: '#Cannot unset offset string on array.+\.#'
 			path: '%rootDir%/../../../src/Utility/Language.php'
 		-