Skip to content

Commit

Permalink
Use sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Feb 21, 2024
1 parent 3e207c9 commit a37859d
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 37 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: pcov
coverage: xdebug
tools: composer:v2

- name: Install dependencies
run: composer update ${{ matrix.deps_strategy }}

- name: Tests
run: vendor/bin/phpunit --colors=always
run: vendor/bin/phpunit --colors=always --coverage-clover=coverage.xml

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ Front Matter
============

[![Packagist](https://img.shields.io/packagist/v/webuni/front-matter.svg?style=flat-square)](https://packagist.org/packages/webuni/front-matter)
[![Build Status](https://img.shields.io/github/workflow/status/webuni/front-matter/Tests/master.svg?style=flat-square)](https://github.com/webuni/front-matter/actions?query=workflow%3ATests+branch%3Amaster)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/quality/g/webuni/front-matter?style=flat-square)](https://scrutinizer-ci.com/g/webuni/front-matter/?branch=master)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/webuni/front-matter?style=flat-square)](https://scrutinizer-ci.com/g/webuni/front-matter/?branch=master)
[![Build Status](https://img.shields.io/github/workflow/status/webuni/front-matter/Tests/main.svg?style=flat-square)](https://github.com/webuni/front-matter/actions?query=workflow%3ATests+branch%3Amain)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=webuni_front-matter&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=webuni_front-matter)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=webuni_front-matter&metric=coverage)](https://sonarcloud.io/summary/new_code?id=webuni_front-matter)

The most universal Front matter (yaml, json, neon, toml) parser and dumper for PHP.
Front matter allows page-specific variables to be included at the top of a page.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"league/commonmark": "^2.0",
"mthaml/mthaml": "^1.3",
"nette/neon": "^2.2 || ^3.0",
"phpunit/phpunit": "^9 || ^10",
"phpunit/phpunit": "^9.6",
"phpstan/phpstan": "^1.10",
"twig/twig": "^3.0",
"yosymfony/toml": "^1.0"
Expand Down
9 changes: 7 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">
<coverage/>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="Webuni Front matter Test Suite">
<testsuite name="Webuni Front Matter Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
6 changes: 6 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sonar.organization=webuni
sonar.projectKey=webuni_front-matter

sonar.sources=src
sonar.tests=tests
sonar.php.coverage.reportPaths=coverage.xml
12 changes: 10 additions & 2 deletions src/FrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public function __construct(ProcessorInterface $processor = null, string $startS
$this->startSep = $startSep;
$this->endSep = $endSep;
$this->processor = $processor ?: new YamlProcessor();

$this->regexp = '{^(?:'.preg_quote($startSep).")[\r\n|\n]*(.*?)[\r\n|\n]+(?:".preg_quote($endSep).")[\r\n|\n]*(.*)$}s";
$this->regexp = $this->getRegExp($startSep, $endSep);
}

/**
Expand Down Expand Up @@ -111,4 +110,13 @@ private function revealIndention(string $string): string

return '';
}

private function getRegExp(string $startSep, string $endSep): string
{
$startSepQuoted = preg_quote($startSep);
$endSepQuoted = preg_quote($endSep);
$newline = '[\r\n|\n]';

return "{^(?:{$startSepQuoted}){$newline}*(.*?){$newline}+(?:{$endSepQuoted}){$newline}*(.*)$}s";
}
}
7 changes: 5 additions & 2 deletions src/FrontMatterChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Webuni\FrontMatter;

use InvalidArgumentException;
use Webuni\FrontMatter\Pug\PugCommentFrontMatter;
use Webuni\FrontMatter\Twig\TwigCommentFrontMatter;

Expand All @@ -24,14 +25,16 @@ public function __construct(iterable $adapters)
{
foreach ($adapters as $adapter) {
if (!$adapter instanceof FrontMatterInterface) {
throw new \InvalidArgumentException('Adapter should be instance of '.FrontMatterInterface::class);
throw new InvalidArgumentException('Adapter should be instance of '.FrontMatterInterface::class);
}

$this->adapters[] = $adapter;
}

if (empty($this->adapters)) {
throw new \InvalidArgumentException('It is necessary add at least one front matter adapter '.FrontMatterInterface::class);
throw new InvalidArgumentException(
'It is necessary add at least one front matter adapter '.FrontMatterInterface::class
);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Pug/PugCommentFrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class PugCommentFrontMatter
{
private function __construct()
{
// prevent any instantiation
}

public static function createWithEndComment(ProcessorInterface $processor = null): FrontMatter
Expand Down
15 changes: 10 additions & 5 deletions src/Twig/DataToTwigConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Webuni\FrontMatter\Twig;

use DateTimeInterface;

class DataToTwigConvertor
{
/** @var callable */
Expand All @@ -24,7 +26,6 @@ public function __invoke(array $data): string
return (string) $convertor($data);
}


protected function __construct(callable $convertor)
{
$this->convertor = $convertor;
Expand All @@ -37,7 +38,7 @@ protected function __construct(callable $convertor)
*/
public static function nothing(): self
{
return new self(function (array $data) {
return new self(function () {
return '';
});
}
Expand All @@ -55,7 +56,9 @@ public static function vars(bool $force = true): self
if (is_int($key)) {
continue;
}
$content .= "{% set $key = " . ($force ? '' : "$key is defined ? $key : ") . self::valueToTwig($value) . " %}\n";

$value = ($force ? '' : "$key is defined ? $key : ") . self::valueToTwig($value);
$content .= "{% set {$key} = {$value} %}\n";
}

return $content;
Expand All @@ -72,7 +75,9 @@ public static function vars(bool $force = true): self
public static function var(string $name, bool $force = true): self
{
return new self(function (array $data) use ($name, $force) {
return "{% set $name = " . ($force ? '' : "$name is defined ? $name : ") . self::valueToTwig($data) . "%}\n";
$value = ($force ? '' : "$name is defined ? $name : ") . self::valueToTwig($data);

return "{% set {$name} = {$value} %}\n";
});
}

Expand All @@ -83,7 +88,7 @@ public static function var(string $name, bool $force = true): self
*/
protected static function valueToTwig($value): string
{
if ($value instanceof \DateTimeInterface) {
if ($value instanceof DateTimeInterface) {
return '(' . $value->getTimestamp() . "|date_modify('0sec'))";
}

Expand Down
6 changes: 5 additions & 1 deletion src/Twig/FrontMatterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class FrontMatterLoader implements LoaderInterface
/** @var DataToTwigConvertor */
private $convertor;

public function __construct(FrontMatterInterface $parser, LoaderInterface $loader, DataToTwigConvertor $convertor = null)
public function __construct(
FrontMatterInterface $parser,
LoaderInterface $loader,
DataToTwigConvertor $convertor = null
)
{
$this->loader = $loader;
$this->parser = $parser;
Expand Down
1 change: 1 addition & 0 deletions src/Twig/TwigCommentFrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class TwigCommentFrontMatter
{
private function __construct()
{
// prevent any instantiation
}

public static function create(ProcessorInterface $processor = null): FrontMatter
Expand Down
4 changes: 3 additions & 1 deletion tests/FrontMatterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ protected function setUp(): void
public function testEmptyAdapters(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('It is necessary add at least one front matter adapter '.FrontMatterInterface::class);
$this->expectExceptionMessage(
'It is necessary add at least one front matter adapter '.FrontMatterInterface::class
);

new FrontMatterChain([]);
}
Expand Down
7 changes: 6 additions & 1 deletion tests/FrontMatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public function testYaml(string $string, array $data, string $content, bool $has
/**
* @dataProvider getSeparator
*/
public function testYamlWithCustomSeparator(string $string, array $data, string $content, bool $hasFrontMatter): void
public function testYamlWithCustomSeparator(
string $string,
array $data,
string $content,
bool $hasFrontMatter
): void
{
$frontMatter = new FrontMatter(null, '<!--', '-->');
$document = $frontMatter->parse($string);
Expand Down
20 changes: 3 additions & 17 deletions tests/Twig/DataToTwigConvertorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,21 @@ public function testNothing(): void
public function testVars(): void
{
$convertor = DataToTwigConvertor::vars();
$twig = '{% set foo = "bar" %}
{% set number = 1234 %}
{% set pi = 3.14159 %}
{% set date = (1464307200|date_modify(\'0sec\')) %}
{% set empty = null %}
{% set multiline = "Multiple\nLine\nString\n" %}
{% set object = {key: "value", datetime: (1605185652|date_modify(\'0sec\')), values: {0: "one", 1: "two", }, } %}
';
$twig = file_get_contents(__DIR__.'/templates/vars.twig');
self::assertEquals($twig, $convertor($this->data));
}

public function testOptionalVars(): void
{
$convertor = DataToTwigConvertor::vars(false);
$twig = '{% set foo = foo is defined ? foo : "bar" %}
{% set number = number is defined ? number : 1234 %}
{% set pi = pi is defined ? pi : 3.14159 %}
{% set date = date is defined ? date : (1464307200|date_modify(\'0sec\')) %}
{% set empty = empty is defined ? empty : null %}
{% set multiline = multiline is defined ? multiline : "Multiple\nLine\nString\n" %}
{% set object = object is defined ? object : {key: "value", datetime: (1605185652|date_modify(\'0sec\')), values: {0: "one", 1: "two", }, } %}
';
$twig = file_get_contents(__DIR__.'/templates/optionalvars.twig');
self::assertEquals($twig, $convertor($this->data));
}

public function testVar(): void
{
$convertor = DataToTwigConvertor::var('parameters');
$twig = '{% set parameters = {foo: "bar", number: 1234, pi: 3.14159, date: (1464307200|date_modify(\'0sec\')), empty: null, multiline: "Multiple\nLine\nString\n", object: {key: "value", datetime: (1605185652|date_modify(\'0sec\')), values: {0: "one", 1: "two", }, }, }%}'."\n";
$twig = file_get_contents(__DIR__.'/templates/var.twig');
self::assertEquals($twig, $convertor($this->data));
}
}
7 changes: 7 additions & 0 deletions tests/Twig/templates/optionalvars.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% set foo = foo is defined ? foo : "bar" %}
{% set number = number is defined ? number : 1234 %}
{% set pi = pi is defined ? pi : 3.14159 %}
{% set date = date is defined ? date : (1464307200|date_modify('0sec')) %}
{% set empty = empty is defined ? empty : null %}
{% set multiline = multiline is defined ? multiline : "Multiple\nLine\nString\n" %}
{% set object = object is defined ? object : {key: "value", datetime: (1605185652|date_modify('0sec')), values: {0: "one", 1: "two", }, } %}
1 change: 1 addition & 0 deletions tests/Twig/templates/var.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% set parameters = {foo: "bar", number: 1234, pi: 3.14159, date: (1464307200|date_modify('0sec')), empty: null, multiline: "Multiple\nLine\nString\n", object: {key: "value", datetime: (1605185652|date_modify('0sec')), values: {0: "one", 1: "two", }, }, } %}
7 changes: 7 additions & 0 deletions tests/Twig/templates/vars.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% set foo = "bar" %}
{% set number = 1234 %}
{% set pi = 3.14159 %}
{% set date = (1464307200|date_modify('0sec')) %}
{% set empty = null %}
{% set multiline = "Multiple\nLine\nString\n" %}
{% set object = {key: "value", datetime: (1605185652|date_modify('0sec')), values: {0: "one", 1: "two", }, } %}

0 comments on commit a37859d

Please sign in to comment.