Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Changed Twig class names and namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Apr 12, 2019
1 parent 88b3b96 commit dc75511
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 123 deletions.
42 changes: 42 additions & 0 deletions .cs.php
@@ -0,0 +1,42 @@
<?php

return PhpCsFixer\Config::create()
->setUsingCache(false)
->setRiskyAllowed(true)
//->setCacheFile(__DIR__ . '/.php_cs.cache')
->setRules([
'@PSR1' => true,
'@PSR2' => true,
'@Symfony' => true,
'psr4' => true,
// custom rules
'align_multiline_comment' => true, // psr-5
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'cast_spaces' => ['space' => 'none'],
'concat_space' => ['spacing' => 'one'],
'compact_nullable_typehint' => true,
'declare_equal_normalize' => ['space' => 'single'],
'general_phpdoc_annotation_remove' => [
'annotations' => [
'author',
'package',
],
],
'increment_style' => ['style' => 'post'],
'list_syntax' => ['syntax' => 'long'],
'no_short_echo_tag' => true,
'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
'phpdoc_align' => false,
'phpdoc_no_empty_return' => false,
'phpdoc_order' => true, // psr-5
'protected_to_private' => false,
'yoda_style' => false,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
])
->setFinder(PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true));
48 changes: 21 additions & 27 deletions .scrutinizer.yml
@@ -1,46 +1,40 @@
filter:
excluded_paths:
- "tests/"
- "resources/"
- "vendor/"
- "src/CssMin/"
- "src/JsMin/"
paths: ["src/*"]
excluded_paths: ["vendor/*", "tests/*"]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
code_rating: true
duplication: true

tools:
external_code_coverage:
timeout: 600
runs: 1
external_code_coverage: false

build:
environment:
php: 7.1.1
mysql: true
php: 7.2
mysql: false
node: false
postgresql: false
mongodb: false
elasticsearch: false
redis: false
memcached: false
neo4j: false
rabbitmq: false
nodes:
analysis:
tests:
override:
- php-scrutinizer-run
dependencies:
before:
- composer self-update
- composer update --no-interaction --prefer-dist --no-progress
tests:
override:
before:
-
command: 'vendor/bin/phpunit --coverage-clover build/logs/clover.xml'
command: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
coverage:
file: 'build/logs/clover.xml'
format: 'clover'
format: 'clover'
21 changes: 10 additions & 11 deletions .travis.yml
Expand Up @@ -3,25 +3,24 @@ language: php
php:
- 7.1
- 7.2
- 7.3

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false
dist: xenial

## Cache composer
addons:
apt:
packages:
- ant

# Cache composer
cache:
directories:
- $HOME/.composer/cache

before_script:
- travis_retry composer self-update
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
- travis_retry composer install ${COMPOSER_FLAGS} --no-interaction --prefer-dist --no-progress
- cd $TRAVIS_BUILD_DIR

script:
- ant phpstan
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_success:
- if [ "$TRAVIS_PHP_VERSION" == "7.1" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.1" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
- ant check-all
70 changes: 43 additions & 27 deletions build.xml
Expand Up @@ -5,17 +5,31 @@
<os family="windows"/>
</condition>

<target name="phpunit" description="Run PHPUnit">
<exec executable="${basedir}/vendor/bin/phpunit${ext}" searchpath="true" resolveexecutable="true" failonerror="true" taskname="phpunit">
<target name="lint">
<apply executable="php" failonerror="true" verbose="true">
<arg value="-l"/>
<fileset dir="${basedir}">
<include name="**/src/**/*.php"/>
<exclude name="**/vendor/**"/>
<modified/>
</fileset>
</apply>
</target>

<target name="phpunit" description="Run unit tests with PHPUnit">
<exec executable="${basedir}/vendor/bin/phpunit${ext}" searchpath="true" resolveexecutable="true"
failonerror="true" taskname="phpunit">
<arg value="--configuration"/>
<arg path="${basedir}/phpunit.xml"/>
</exec>
</target>

<target name="phpunit-coverage" description="Run PHPUnit with coverage">
<target name="phpunit-coverage" description="Run unit tests with PHPUnit with coverage">
<delete dir="${basedir}/build/coverage"/>
<mkdir dir="${basedir}/build/coverage"/>
<exec executable="${basedir}/vendor/bin/phpunit${ext}" searchpath="true" resolveexecutable="true" failonerror="true" taskname="phpunit-coverage">
<exec executable="${basedir}/vendor/bin/phpunit${ext}" searchpath="true" resolveexecutable="true"
failonerror="true" taskname="phpunit-coverage">
<arg value="--coverage-text"/>
<arg value="--configuration"/>
<arg path="${basedir}/phpunit.xml"/>
<arg value="--coverage-clover"/>
Expand All @@ -25,34 +39,13 @@
</exec>
</target>

<target name="fix-style" description="Code style fixer">
<mkdir dir="${basedir}/build"/>
<get src="https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.10.2/php-cs-fixer.phar" dest="${basedir}/build/php-cs-fixer.phar" skipexisting="true"/>
<exec executable="php" searchpath="true" resolveexecutable="true">
<arg value="${basedir}/build/php-cs-fixer.phar"/>
<arg value="fix"/>
<arg value="--rules=@PSR2"/>
<arg value="--using-cache=no"/>
<arg path="${basedir}/src"/>
</exec>
<exec executable="php" searchpath="true" resolveexecutable="true">
<arg value="${basedir}/build/php-cs-fixer.phar"/>
<arg value="fix"/>
<arg value="--rules=@PSR2"/>
<arg value="--using-cache=no"/>
<arg path="${basedir}/tests"/>
</exec>
</target>

<target name="phpstan" description="PHP Static Analysis Tool - discover bugs in your code without running it">
<mkdir dir="${basedir}/build"/>
<get src="https://github.com/phpstan/phpstan/releases/download/0.9.2/phpstan.phar"
dest="${basedir}/build/phpstan.phar" skipexisting="true"/>
<exec executable="php" searchpath="true" resolveexecutable="true" failonerror="true">
<arg value="${basedir}/build/phpstan.phar"/>
<arg value="${basedir}/vendor/phpstan/phpstan-shim/phpstan.phar"/>
<arg value="analyse"/>
<arg value="-l"/>
<arg value="1"/>
<arg value="5"/>
<arg value="-c"/>
<arg path="${basedir}/phpstan.neon"/>
<arg value="src"/>
Expand All @@ -61,4 +54,27 @@
<arg value="--no-progress"/>
</exec>
</target>

<target name="fix-style" description="Code style fixer">
<mkdir dir="${basedir}/build"/>
<get src="https://cs.symfony.com/download/php-cs-fixer-v2.phar" dest="${basedir}/build/php-cs-fixer.phar"
skipexisting="true"/>
<exec executable="php" searchpath="true" resolveexecutable="true" failonerror="true">
<arg value="${basedir}/build/php-cs-fixer.phar"/>
<arg line="fix --config=.cs.php"/>
</exec>
</target>

<target name="check-style" description="Code style checker">
<mkdir dir="${basedir}/build"/>
<get src="https://cs.symfony.com/download/php-cs-fixer-v2.phar" dest="${basedir}/build/php-cs-fixer.phar"
skipexisting="true"/>
<exec executable="php" searchpath="true" resolveexecutable="true" failonerror="true">
<arg value="${basedir}/build/php-cs-fixer.phar"/>
<arg line="fix --dry-run --format=txt --verbose --diff --diff-format=udiff --config=.cs.php"/>
</exec>
</target>

<target name="check-all" depends="lint,check-style,phpstan,phpunit-coverage"/>

</project>
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -25,7 +25,8 @@
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"mikey179/vfsStream": "^1.6"
"mikey179/vfsStream": "^1.6",
"phpstan/phpstan-shim": "^0.11"
},
"scripts": {
"test": "phpunit",
Expand Down
47 changes: 28 additions & 19 deletions src/Twig/TwigCompiler.php
Expand Up @@ -7,17 +7,18 @@
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RuntimeException;
use SplFileInfo;
use Twig_Environment;
use Twig_Loader_Filesystem;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

/**
* Class TwigCompiler
* Class TwigCompiler.
*/
class TwigCompiler
{
/**
* @var Twig_Environment
* @var Environment
*/
private $twig;

Expand All @@ -29,14 +30,15 @@ class TwigCompiler
/**
* The constructor.
*
* @param Twig_Environment $twig The Twig Environment instance
* @param Environment $twig The Twig Environment instance
* @param string $cachePath The twig cache path
*
* @throws Exception
*/
public function __construct(Twig_Environment $twig, string $cachePath)
public function __construct(Environment $twig, string $cachePath)
{
if (empty($cachePath)) {
throw new Exception('The cache path cannot be empty');
throw new RuntimeException('The cache path cannot be empty');
}

$this->twig = $twig;
Expand All @@ -46,10 +48,11 @@ public function __construct(Twig_Environment $twig, string $cachePath)
/**
* Compile all twig templates.
*
* @return bool Success
* @throws Exception Exception
*
* @return bool Success
*/
public function compile()
public function compile(): bool
{
// Delete old twig cache files
if (file_exists($this->cachePath)) {
Expand All @@ -65,15 +68,17 @@ public function compile()
$this->twig->enableAutoReload();

if (!$this->twig->getCache()) {
throw new Exception('The Twig cache must be enabled!');
throw new RuntimeException('The Twig cache must be enabled!');
}

/* @var Twig_Loader_Filesystem $loader */
$loader = $this->twig->getLoader();
$paths = $loader->getPaths();

foreach ($paths as $path) {
$this->compileFiles($path);
if ($loader instanceof FilesystemLoader) {
$paths = $loader->getPaths();

foreach ($paths as $path) {
$this->compileFiles($path);
}
}

return true;
Expand All @@ -83,12 +88,15 @@ public function compile()
* Compile Twig files.
*
* @param string $viewPath
*
* @return void
*/
private function compileFiles($viewPath)
private function compileFiles(string $viewPath)
{
$directory = new RecursiveDirectoryIterator($viewPath, FilesystemIterator::SKIP_DOTS);

foreach (new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST) as $file) {
/* @var SplFileInfo $file */
/** @var SplFileInfo $file */
if ($file->isFile() && $file->getExtension() === 'twig') {
$templateName = substr($file->getPathname(), strlen($viewPath) + 1);
$templateName = str_replace('\\', '/', $templateName);
Expand All @@ -103,9 +111,10 @@ private function compileFiles($viewPath)
* This function is compatible with vfsStream.
*
* @param string $path Path
* @return bool True on success or false on failure.
*
* @return bool true on success or false on failure
*/
private function removeDirectory($path)
private function removeDirectory(string $path): bool
{
$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileInfo) {
Expand All @@ -118,7 +127,7 @@ private function removeDirectory($path)

$files = new FilesystemIterator($path);

/* @var SplFileInfo $file */
/** @var SplFileInfo $file */
foreach ($files as $file) {
$fileName = $file->getPathname();
unlink($fileName);
Expand Down

0 comments on commit dc75511

Please sign in to comment.