Skip to content

Commit

Permalink
feature: php lint added (#33)
Browse files Browse the repository at this point in the history
* feature: php lint added

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* improvement of the code

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* moving debug message to helper

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* refactor: removal of package discover due to stand-alone package

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* adding HtmlCompressor class

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* adding HtmlCompressor class

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* adding HtmlCompressor class

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* refactor: SideEffects fix

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* adding php configuration class and test

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* adding phplint in ci/cd

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* adding yui compressor

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

* adding online reference for the yui compressor

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>

---------

Signed-off-by: Vallabh Kansagara <vrkansagara@gmail.com>
  • Loading branch information
vrkansagara committed Jun 12, 2023
1 parent 28f54a8 commit d660b27
Show file tree
Hide file tree
Showing 18 changed files with 394 additions and 220 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress --ignore-platform-req=php

- name: PHP linting
run: composer run-script php-lint

- name: Copy phpunit, phpcs, psalm files
run: cp phpcs.xml.dist phpcs.xml && cp phpunit.xml.dist phpunit.xml && cp psalm.xml.dist psalm.xml

- name: Run ch-check
- name: Run cs-check
run: composer run-script cs-check

- name: Run cs-fix
Expand Down
7 changes: 7 additions & 0 deletions bin/info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

### yui compresso info

~~~bash
http://yui.github.io/yuicompressor/
cat test.js | java -jar ../bin/yuicompressor-2.4.8.jar --type=js > test.min.js
~~~
8 changes: 8 additions & 0 deletions bin/php-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

find "$1" -type f \
-path "$(pwd)/vendor/*" -prune -o \
-wholename "$(pwd)/*.php" -print0 | \
xargs -0 -n1 -P4 php -l -n -d error_reporting="E_ALL" | \
(! grep -v "No syntax errors detected" )
Binary file added bin/yuicompressor-2.4.8.jar
Binary file not shown.
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"source": "https://github.com/vrkansagara/LaraOutPress"
},
"require": {
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
"php": "~8.2.0",
"ext-zlib": "*",
"ext-pcre": "*"
},
Expand All @@ -35,15 +35,15 @@
},
"autoload": {
"psr-4": {
"Vrkansagara\\LaraOutPress\\": "src/"
"Vrkansagara\\LaraOutPress\\": "src"
},
"files": [
"src/Helpers/helper.php"
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Vrkansagara\\LaraOutPress\\": "tests/"
"Vrkansagara\\LaraOutPress\\Tests\\": "tests"
}
},
"minimum-stability": "dev",
Expand Down Expand Up @@ -87,10 +87,12 @@
],
"cs-check": "./vendor/bin/phpcs -s",
"cs-fix": "./vendor/bin/phpcbf --standard=$(pwd)/phpcs.xml",
"php-lint": "./bin/php-lint $(pwd)",
"test": [
"./vendor/bin/phpunit --testdox --colors=always",
"echo 'Current head at ' && git rev-parse --verify HEAD"
],
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"test-coverage-html": "vendor/bin/phpunit --coverage-html coverage"
}
}
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</rule>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>public/index.php</exclude-pattern>
<exclude-pattern>src/Middleware/AfterMiddleware.php</exclude-pattern>
</rule>

<!-- Paths to check -->
Expand Down
1 change: 1 addition & 0 deletions public/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('Test');
4 changes: 2 additions & 2 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Vrkansagara\LaraOutPress;

/**
* @copyright Copyright (c) 2015-2022 Vallabh Kansagara <vrkansagara@gmail.com>
* @copyright Copyright (c) 2015-2023 Vallabh Kansagara <vrkansagara@gmail.com>
* @license https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

Expand All @@ -14,7 +14,7 @@ class Facade extends \Illuminate\Support\Facades\Facade
/**
* {@inheritDoc}
*/
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): LaraOutPress
{
return LaraOutPress::class;
}
Expand Down
152 changes: 152 additions & 0 deletions src/HtmlCompressor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Vrkansagara\LaraOutPress;

/**
* @copyright Copyright (c) 2015-2023 Vallabh Kansagara <vrkansagara@gmail.com>
* @license https://opensource.org/licenses/BSD-3-Clause New BSD License
*/
class HtmlCompressor
{
public function init(string $content)
{
$whiteSpaceRules = [
'/(\s)+/s' => '\\1',// shorten multiple whitespace sequences
"#>\s+<#" => ">\n<", // Strip excess whitespace using new line
"#\n\s+<#" => "\n<",// strip excess whitespace using new line
'/\>[^\S ]+/s' => '>',
// Strip all whitespaces after tags, except space
'/[^\S ]+\</s' => '<',// strip whitespaces before tags, except space
/**
* '/\s+ # Match one or more whitespace characters
* (?! # but only if it is impossible to match...
* [^<>]* # any characters except angle brackets
* > # followed by a closing bracket.
* ) # End of lookahead
* /x',
*/

//Remove all whitespaces except content between html tags.
//MOST DANGEROUS
// '/\s+(?![^<>]*>)/x' => '',
];
$commentRules = [
"/<!--.*?-->/ms" => '',// Remove all html comment.,
];
$replaceWords = [
//OldWord will be replaced by the NewWord
// OldWord <-> NewWord DO NOT REMOVE THIS LINE. {REFERENCE LINE}
//'/\bOldWord\b/i' =>'NewWord'
];

$allRules = array_merge(
$replaceWords,
$commentRules,
$whiteSpaceRules
);

$buffer = $this->compressJscript($content);
$buffer = preg_replace(
array_keys($allRules),
array_values($allRules),
$buffer
);
return $buffer;
}
/**
* This method will no longer support.
*
* @note Code arked as @deprecated but for reference only.
* @param $buffer
* @return null|string|string[] Compressed output
* @deprecated
*
*/
public static function compress($buffer)
{
/**
* To remove useless whitespace from generated HTML, except for Javascript.
* [Regex Source]
* https://github.com/bcit-ci/codeigniter/wiki/compress-html-output
* http://stackoverflow.com/questions/5312349/minifying-final-html-output-using-regular-expressions-with-codeigniter
* %# Collapse ws everywhere but in blacklisted elements.
* (?> # Match all whitespaces other than single space.
* [^\S ]\s* # Either one [\t\r\n\f\v] and zero or more ws,
* | \s{2,} # or two or more consecutive-any-whitespace.
* ) # Note: The remaining regex consumes no text at all...
* (?= # Ensure we are not in a blacklist tag.
* (?: # Begin (unnecessary) group.
* (?: # Zero or more of...
* [^<]++ # Either one or more non-"<"
* | < # or a < starting a non-blacklist tag.
* (?!/?(?:textarea|pre)\b)
* )*+ # (This could be "unroll-the-loop"ified.)
* ) # End (unnecessary) group.
* (?: # Begin alternation group.
* < # Either a blacklist start tag.
* (?>textarea|pre)\b
* | \z # or end of file.
* ) # End alternation group.
* ) # If we made it here, we are not in a blacklist tag.
* %ix
*/
$regexRemoveWhiteSpace
= '%(?>[^\S ]\s*| \s{2,})(?=(?:(?:[^<]++| <(?!/?(?:textarea|pre)\b))*+)(?:<(?>textarea|pre)\b|\z))%ix';
$new_buffer = preg_replace($regexRemoveWhiteSpace, '', $buffer);
// We are going to check if processing has working
if ($new_buffer === null) {
$new_buffer = $buffer;
}

return $new_buffer;
}

public function compressJscript($buffer)
{
// JavaScript compressor by John Elliot <jj5@jj5.net>
$replace = [
'#\'([^\n\']*?)/\*([^\n\']*)\'#' => "'\1/'+\'\'+'*\2'",
// remove comments from ' strings
'#\"([^\n\"]*?)/\*([^\n\"]*)\"#' => '"\1/"+\'\'+"*\2"',
// remove comments from " strings
'#/\*.*?\*/#s' => "",// strip C style comments
'#[\r\n]+#' => "\n",
// remove blank lines and \r's
'#\n([ \t]*//.*?\n)*#s' => "\n",
// strip line comments (whole line only)
'#([^\\])//([^\'"\n]*)\n#s' => "\\1\n",
// strip line comments
// (that aren't possibly in strings or regex's)
'#\n\s+#' => "\n",// strip excess whitespace
'#\s+\n#' => "\n",// strip excess whitespace
'#(//[^\n]*\n)#s' => "\\1\n",
// extra line feed after any comments left
// (important given later replacements)
'#/([\'"])\+\'\'\+([\'"])\*#' => "/*"
// restore comments in strings
];
$script = preg_replace(array_keys($replace), $replace, $buffer);
$replace = [
"&&\n" => "&&",
"||\n" => "||",
"(\n" => "(",
")\n" => ")",
"[\n" => "[",
"]\n" => "]",
"+\n" => "+",
",\n" => ",",
"?\n" => "?",
":\n" => ":",
";\n" => ";",
"{\n" => "{",
// "}\n" => "}", (because I forget to put semicolons after function assignments)
"\n]" => "]",
"\n)" => ")",
"\n}" => "}",
"\n\n" => "\n",
];
$script = str_replace(array_keys($replace), $replace, $script);
return trim($script);
}
}
Loading

0 comments on commit d660b27

Please sign in to comment.