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: Write license to file #28

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
35 changes: 33 additions & 2 deletions .php_cs
Expand Up @@ -13,9 +13,40 @@ declare(strict_types=1);

use Ergebnis\PhpCsFixer\Config;

$years = Config\License\Copyright\Years::fromYear(Config\License\Copyright\Year::fromString('2019'));
$holder = Config\License\Copyright\Holder::fromString('Andreas Möller');

$file = Config\License\File::create(
$years,
$holder,
Config\License\Template::fromString(
<<<'EOF'
The MIT License (MIT)
Copyright (c) <copyright-years> <copyright-holder>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
EOF
)
);

$file->saveAs(__DIR__ . '/LICENSE');

$header = Config\License\Header::create(
Config\License\Copyright\Years::fromYear(Config\License\Copyright\Year::fromString('2019')),
Config\License\Copyright\Holder::fromString('Andreas Möller'),
$years,
$holder,
Config\License\Notice::fromString(
<<<'EOF'
For the full copyright and license information, please view
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -28,6 +28,7 @@
"phpstan/phpstan-strict-rules": "~0.12.0",
"phpunit/phpunit": "^7.5.18",
"psalm/plugin-phpunit": "~0.8.0",
"symfony/filesystem": "^4.4.0",
"vimeo/psalm": "^3.8.1"
},
"config": {
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

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

58 changes: 58 additions & 0 deletions src/License/File.php
@@ -0,0 +1,58 @@
<?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\License;

/**
* @internal
*/
final class File
{
private $years;

private $holder;

private $template;

private function __construct(Copyright\Years $years, Copyright\Holder $holder, Template $template)
{
$this->years = $years;
$this->holder = $holder;
$this->template = $template;
}

public static function create(Copyright\Years $years, Copyright\Holder $holder, Template $template): self
{
return new self(
$years,
$holder,
$template
);
}

public function toString(): string
{
return $this->template->toString([
'<copyright-years>' => $this->years->toString(),
'<copyright-holder>' => $this->holder->toString(),
]);
}

public function saveAs(string $filename): void
{
\file_put_contents(
$filename,
$this->toString()
);
}
}
41 changes: 41 additions & 0 deletions src/License/Template.php
@@ -0,0 +1,41 @@
<?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\License;

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

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

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

public function toString(array $replacements): string
{
return \str_replace(
\array_keys($replacements),
$replacements,
$this->value
);
}
}
104 changes: 104 additions & 0 deletions test/Unit/License/FileTest.php
@@ -0,0 +1,104 @@
<?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\Test\Unit\License;

use Ergebnis\PhpCsFixer\Config\License\Copyright\Holder;
use Ergebnis\PhpCsFixer\Config\License\Copyright\Year;
use Ergebnis\PhpCsFixer\Config\License\Copyright\Years;
use Ergebnis\PhpCsFixer\Config\License\File;
use Ergebnis\PhpCsFixer\Config\License\Template;
use Ergebnis\Test\Util\Helper;
use PHPUnit\Framework;
use Symfony\Component\Filesystem;

/**
* @internal
*
* @covers \Ergebnis\PhpCsFixer\Config\License\File
*
* @uses \Ergebnis\PhpCsFixer\Config\License\Copyright\Holder
* @uses \Ergebnis\PhpCsFixer\Config\License\Copyright\Year
* @uses \Ergebnis\PhpCsFixer\Config\License\Copyright\Years
* @uses \Ergebnis\PhpCsFixer\Config\License\Template
*/
final class FileTest extends Framework\TestCase
{
use Helper;

protected function setUp(): void
{
$filesystem = new Filesystem\Filesystem();

$filesystem->mkdir(self::directory());
}

protected function tearDown(): void
{
$filesystem = new Filesystem\Filesystem();

$filesystem->remove(self::directory());
}

public function testCreateReturnsFile(): void
{
$faker = self::faker();

$years = Years::fromYear(Year::fromString($faker->dateTime->format('Y')));
$holder = Holder::fromString($faker->name);

$template = Template::fromString(
<<<'EOF'
Ah!
This was done in <copyright-years> by <copyright-holder>.
Who would have thought?
EOF
);

$file = File::create(
$years,
$holder,
$template
);

$expected = <<<EOF
Ah!
This was done in {$years->toString()} by {$holder->toString()}.
Who would have thought?
EOF;

self::assertSame($expected, $file->toString());

$filename = \sprintf(
'%s/%s',
self::directory(),
'example.txt'
);

$file->saveAs($filename);

self::assertFileExists($filename);
self::assertSame($expected, \file_get_contents($filename));
}

private static function directory(): string
{
return __DIR__ . '/../../../.build/test';
}
}
61 changes: 61 additions & 0 deletions test/Unit/License/TemplateTest.php
@@ -0,0 +1,61 @@
<?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\Test\Unit\License;

use Ergebnis\PhpCsFixer\Config\License\Template;
use Ergebnis\Test\Util\Helper;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\PhpCsFixer\Config\License\Template
*/
final class TemplateTest extends Framework\TestCase
{
use Helper;

public function testCreateReturnsTemplate(): void
{
$faker = self::faker();

$when = $faker->dateTime->format('Y');
$who = $faker->name;

$template = Template::fromString(
<<<'EOF'
Ah!
This was done in <when> by <who>.
Who would have thought?
EOF
);

$expected = <<<EOF
Ah!
This was done in {$when} by {$who}.
Who would have thought?
EOF;

self::assertSame($expected, $template->toString([
'<when>' => $when,
'<who>' => $who,
]));
}
}