Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Allow construction of header #23

Merged
merged 1 commit into from Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Expand Up @@ -11,7 +11,7 @@ on:
env:
MIN_COVERED_MSI: 100
MIN_MSI: 100
REQUIRED_PHP_EXTENSIONS: "mbstring"
REQUIRED_PHP_EXTENSIONS: "filter, mbstring"

jobs:
coding-standards:
Expand Down
17 changes: 10 additions & 7 deletions .php_cs
Expand Up @@ -13,16 +13,19 @@ declare(strict_types=1);

use Ergebnis\PhpCsFixer\Config;

$header = <<<'EOF'
Copyright (c) 2019 Andreas Möller
$header = Config\Header\Header::create(
Config\Header\CopyrightYears::fromYear(Config\Header\Year::fromString('2019')),
Config\Header\Author::fromString('Andreas Möller'),
Config\Header\License::fromString(
<<<'EOF'
For the full copyright and license information, please view
the LICENSE file that was distributed with this source code.
EOF
),
Config\Header\Url::fromString('https://github.com/ergebnis/php-cs-fixer-config')
);

@see https://github.com/ergebnis/php-cs-fixer-config
EOF;

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71($header));
$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71($header->toString()));

$config->getFinder()
->ignoreDotFiles(false)
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,10 @@ For a full diff see [`1.1.2...master`][1.1.2...master].

For a full diff see [`1.1.1...1.1.2`][1.1.1...1.1.2].

### Added

* Allowed construction of header ([#23]), by [@localheinz]

### Fixed

* Brought back support for PHP 7.1 ([#17]), by [@localheinz]
Expand Down Expand Up @@ -50,6 +54,7 @@ For a full diff see [`d899e77...1.0.0`][d899e77...1.0.0].
[#3]: https://github.com/ergebnis/php-cs-fixer-config/pull/3
[#14]: https://github.com/ergebnis/php-cs-fixer-config/pull/14
[#17]: https://github.com/ergebnis/php-cs-fixer-config/pull/17
[#23]: https://github.com/ergebnis/php-cs-fixer-config/pull/23

[@linuxjuggler]: https://github.com/linuxjuggler
[@localheinz]: https://github.com/localheinz
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -12,6 +12,7 @@
],
"require": {
"php": "^7.1",
"ext-filter": "*",
"friendsofphp/php-cs-fixer": "~2.16.0"
},
"require-dev": {
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

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

50 changes: 50 additions & 0 deletions src/Header/Author.php
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\Header;

/**
* @internal
*/
final class Author
{
private $value;

private function __construct(string $value)
{
$this->value = $value;
}

/**
* @param string $value
*
* @throws \InvalidArgumentException
*
* @return self
*/
public static function fromString(string $value): self
{
$trimmed = \trim($value);

if ('' === $trimmed) {
throw new \InvalidArgumentException('Value cannot be blank or empty.');
}

return new self($trimmed);
}

public function toString(): string
{
return $this->value;
}
}
66 changes: 66 additions & 0 deletions src/Header/CopyrightYears.php
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\Header;

/**
* @internal
*/
final class CopyrightYears
{
private $value;

private function __construct(string $value)
{
$this->value = $value;
}

/**
* @param Year $startYear
* @param Year $endYear
*
* @throws \InvalidArgumentException
*
* @return self
*/
public static function fromRange(Year $startYear, Year $endYear): self
{
if ($startYear->greaterThan($endYear)) {
throw new \InvalidArgumentException(\sprintf(
'Start year "%s" needs to be equal to or less than end year "%s".',
$startYear->toString(),
$endYear->toString()
));
}

if ($startYear->equals($endYear)) {
return self::fromYear($startYear);
}

return new self(\sprintf(
'%s-%s',
$startYear->toString(),
$endYear->toString()
));
}

public static function fromYear(Year $year): self
{
return new self($year->toString());
}

public function toString(): string
{
return $this->value;
}
}
65 changes: 65 additions & 0 deletions src/Header/Header.php
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\Header;

/**
* @internal
*/
final class Header
{
private $copyrightYear;

private $author;

private $license;

private $url;

private function __construct(CopyrightYears $copyrightYear, Author $author, License $license, Url $url)
{
$this->copyrightYear = $copyrightYear;
$this->author = $author;
$this->license = $license;
$this->url = $url;
}

public static function create(CopyrightYears $copyrightYear, Author $author, License $license, Url $url): self
{
return new self(
$copyrightYear,
$author,
$license,
$url
);
}

public function toString(): string
{
if ($this->license->isEmpty()) {
return <<<EOF
Copyright (c) {$this->copyrightYear->toString()} {$this->author->toString()}

@see {$this->url->toString()}
EOF;
}

return <<<EOF
Copyright (c) {$this->copyrightYear->toString()} {$this->author->toString()}

{$this->license->toString()}

@see {$this->url->toString()}
EOF;
}
}
42 changes: 42 additions & 0 deletions src/Header/License.php
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\Header;

/**
* @internal
*/
final class License
{
private $value;

private function __construct(string $value)
{
$this->value = $value;
}

public static function fromString(string $value): self
{
return new self(\trim($value));
}

public function isEmpty(): bool
{
return '' === $this->value;
}

public function toString(): string
{
return $this->value;
}
}
53 changes: 53 additions & 0 deletions src/Header/Url.php
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ergebnis/php-cs-fixer-config
*/

namespace Ergebnis\PhpCsFixer\Config\Header;

/**
* @internal
*/
final class Url
{
private $value;

private function __construct(string $value)
{
$this->value = $value;
}

/**
* @param string $value
*
* @throws \InvalidArgumentException
*
* @return self
*/
public static function fromString(string $value): self
{
$trimmed = \trim($value);

if (false === \filter_var($trimmed, \FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException(\sprintf(
'Value "%s" does not appear to be a valid URL.',
$value
));
}

return new self($trimmed);
}

public function toString(): string
{
return $this->value;
}
}