diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..3277593 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,47 @@ +name: PHP Code Quality +on: + pull_request: + branches: + - trunk + push: + branches: + - trunk + +jobs: + quality: + name: Code Quality PHP (${{matrix.php_versions}}) + runs-on: ubuntu-latest + strategy: + matrix: + php_versions: ['7.4', '8.0', '8.1', '8.2'] + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP ${{ matrix.php_versions}} + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: ${{matrix.php_versions}} + tools: composer, cs2pr, parallel-lint, phpcs, phpstan, psalm + + - name: Get Composer Cache Directory + id: composer-cache + run: | + echo "::set-output name=dir::$(composer config cache-files-dir)" + - uses: actions/cache@v3 + name: Cache Composer Dependencies + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + - name: Install Composer dependencies + uses: ramsey/composer-install@v1 + + - name: Linting + run: | + parallel-lint --no-colors --no-progress --checkstyle ./Common.php | cs2pr + - name: Run Static Analysis (PHPStan) + run: phpstan analyse \ No newline at end of file diff --git a/Common.php b/Common.php index e2bf9d0..35d0316 100644 --- a/Common.php +++ b/Common.php @@ -30,21 +30,21 @@ * @version Release: @package_version@ * @abstract */ -class HTML_Common +abstract class HTML_Common { /** * Associative array of attributes * @var array * @access private */ - var $_attributes = array(); + protected $_attributes = []; /** * Tab offset of the tag * @var int * @access private */ - var $_tabOffset = 0; + protected $_tabOffset = 0; /** * Tab string @@ -52,7 +52,7 @@ class HTML_Common * @since 1.7 * @access private */ - var $_tab = "\11"; + protected $_tab = "\11"; /** * Contains the line end string @@ -60,7 +60,7 @@ class HTML_Common * @since 1.7 * @access private */ - var $_lineEnd = "\12"; + protected $_lineEnd = "\12"; /** * HTML comment on the object @@ -68,7 +68,7 @@ class HTML_Common * @since 1.5 * @access private */ - var $_comment = ''; + protected $_comment = ''; /** * Class constructor @@ -77,7 +77,7 @@ class HTML_Common * @param int $tabOffset Indent offset in tabs * @access public */ - function HTML_Common($attributes = null, $tabOffset = 0) + public function __construct($attributes = null, int $tabOffset = 0) { $this->setAttributes($attributes); $this->setTabOffset($tabOffset); @@ -88,7 +88,7 @@ function HTML_Common($attributes = null, $tabOffset = 0) * @access public * @returns double */ - function apiVersion() + public function apiVersion(): float { return 1.7; } // end func apiVersion @@ -100,7 +100,7 @@ function apiVersion() * @access private * @return string */ - function _getLineEnd() + protected function _getLineEnd() { return $this->_lineEnd; } // end func getLineEnd @@ -112,7 +112,7 @@ function _getLineEnd() * @access private * @return string */ - function _getTab() + protected function _getTab() { return $this->_tab; } // end func _getTab @@ -123,7 +123,7 @@ function _getTab() * @return string * @access private */ - function _getTabs() + protected function _getTabs() { return str_repeat($this->_getTab(), $this->_tabOffset); } // end func _getTabs @@ -134,12 +134,12 @@ function _getTabs() * @return string * @access private */ - function _getAttrString($attributes) + protected function _getAttrString(array $attributes) { $strAttr = ''; if (is_array($attributes)) { - $charset = HTML_Common::charset(); + $charset = self::charset(); foreach ($attributes as $key => $value) { $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"'; } @@ -153,7 +153,7 @@ function _getAttrString($attributes) * @access private * @return array */ - function _parseAttributes($attributes) + protected function _parseAttributes($attributes) { if (is_array($attributes)) { $ret = array(); @@ -167,7 +167,7 @@ function _parseAttributes($attributes) } return $ret; - } elseif (is_string($attributes)) { + } else if (is_string($attributes)) { $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" . "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/"; if (preg_match_all($preg, $attributes, $regs)) { @@ -188,6 +188,7 @@ function _parseAttributes($attributes) return $arrAttr; } } + return []; } // end func _parseAttributes /** @@ -199,7 +200,7 @@ function _parseAttributes($attributes) * @access private * @return bool */ - function _getAttrKey($attr, $attributes) + protected function _getAttrKey(string $attr, array $attributes) { if (isset($attributes[strtolower($attr)])) { return true; @@ -214,7 +215,7 @@ function _getAttrKey($attr, $attributes) * @param array $attr2 New attributes array * @access private */ - function _updateAttrArray(&$attr1, $attr2) + protected function _updateAttrArray(array &$attr1, array $attr2) { if (!is_array($attr2)) { return false; @@ -233,7 +234,7 @@ function _updateAttrArray(&$attr1, $attr2) * @access private * @return void */ - function _removeAttr($attr, &$attributes) + protected function _removeAttr(string $attr, array &$attributes) { $attr = strtolower($attr); if (isset($attributes[$attr])) { @@ -249,7 +250,7 @@ function _removeAttr($attr, &$attributes) * @access public * @return string|null returns null if an attribute does not exist */ - function getAttribute($attr) + public function getAttribute(string $attr) { $attr = strtolower($attr); if (isset($this->_attributes[$attr])) { @@ -265,7 +266,7 @@ function getAttribute($attr) * @param string Attribute value (will be set to $name if omitted) * @access public */ - function setAttribute($name, $value = null) + public function setAttribute(string $name, ?string $value = null) { $name = strtolower($name); if (is_null($value)) { @@ -279,7 +280,7 @@ function setAttribute($name, $value = null) * @param mixed $attributes Either a typical HTML attribute string or an associative array * @access public */ - function setAttributes($attributes) + public function setAttributes($attributes) { $this->_attributes = $this->_parseAttributes($attributes); } // end func setAttributes @@ -292,7 +293,7 @@ function setAttributes($attributes) * @access public * @return mixed attributes */ - function getAttributes($asString = false) + public function getAttributes(bool $asString = false) { if ($asString) { return $this->_getAttrString($this->_attributes); @@ -306,7 +307,7 @@ function getAttributes($asString = false) * @param mixed $attributes Either a typical HTML attribute string or an associative array * @access public */ - function updateAttributes($attributes) + public function updateAttributes($attributes) { $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes)); } // end func updateAttributes @@ -319,7 +320,7 @@ function updateAttributes($attributes) * @access public * @return void */ - function removeAttribute($attr) + public function removeAttribute($attr) { $this->_removeAttr($attr, $this->_attributes); } //end func removeAttribute @@ -332,7 +333,7 @@ function removeAttribute($attr) * @access public * @return void */ - function setLineEnd($style) + public function setLineEnd(string $style) { switch ($style) { case 'win': @@ -355,7 +356,7 @@ function setLineEnd($style) * @param int $offset * @access public */ - function setTabOffset($offset) + public function setTabOffset(int $offset) { $this->_tabOffset = $offset; } // end func setTabOffset @@ -367,7 +368,7 @@ function setTabOffset($offset) * @access public * @return int */ - function getTabOffset() + public function getTabOffset() { return $this->_tabOffset; } //end func getTabOffset @@ -380,7 +381,7 @@ function getTabOffset() * @access public * @return void */ - function setTab($string) + public function setTab(string $string) { $this->_tab = $string; } // end func setTab @@ -393,7 +394,7 @@ function setTab($string) * @access public * @return void */ - function setComment($comment) + public function setComment(string $comment) { $this->_comment = $comment; } // end func setHtmlComment @@ -405,7 +406,7 @@ function setComment($comment) * @access public * @return string */ - function getComment() + public function getComment() { return $this->_comment; } //end func getComment @@ -417,17 +418,14 @@ function getComment() * @return string * @abstract */ - function toHtml() - { - return ''; - } // end func toHtml + abstract public function toHtml(): string; /** * Displays the HTML to the screen * * @access public */ - function display() + public function display() { print $this->toHtml(); } // end func display @@ -452,7 +450,7 @@ function display() * @access public * @static */ - function charset($newCharset = null) + public static function charset(?string $newCharset = null) { static $charset = 'ISO-8859-1'; diff --git a/composer.json b/composer.json index 54ddca8..2f20c17 100644 --- a/composer.json +++ b/composer.json @@ -35,9 +35,11 @@ }, "type": "library", "require": { + "php": ">=7.4", "pear/pear_exception": "*" }, "require-dev": { - "phpunit/phpunit": "*" + "phpunit/phpunit": "*", + "phpstan/phpstan": "^1.7" } -} \ No newline at end of file +} diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..158fd9b --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + level: 0 + paths: + - ./Common.php \ No newline at end of file