Skip to content

Commit

Permalink
Enhancement: Add None license type
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Oct 1, 2022
1 parent 6662af2 commit ed520d6
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For a full diff see [`1.2.0...main`][1.2.0...main].
## Added

- Added `Header::createWithoutReferenceToLicenseFile()` ([#534]), by [@localheinz]
- Added `None` ([#535]), by [@localheinz]

## Changed

Expand Down
53 changes: 52 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,22 @@ With [`friendsofphp/php-cs-fixer`](https://github.com/FriendsOfPHP/PHP-CS-Fixer)
- save the license to a file, e.g. `LICENSE` or `LICENSE.md`
- specify a file-level header using the `header_comment` fixer that will be replaced in PHP files

Here's an example of a `.php-cs-fixer.php` file:
Here's an example of a `.php-cs-fixer.php` file for an open-source project using the [`MIT`](src/Type/MIT.php) license type:

```php
<?php

declare(strict_types=1);

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

use Ergebnis\License;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
Expand Down Expand Up @@ -74,6 +83,47 @@ return Config::create()

:bulb: Also take a look at [`.php-cs-fixer.php`](.php-cs-fixer.php) of this project.

Here's an example of a `.php-cs-fixer.php` file for a closed-source project using the [`None`](src/Type/None.php) license type:


```php
<?php

declare(strict_types=1);

/**
* Copyright (c) 2011-2019 Andreas Möller
*
* @see https://github.com/localheinz/localheinz.com
*/

use Ergebnis\License;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$license = License\Type\None::text(
License\Range::since(
License\Year::fromString('2020'),
new \DateTimeZone('UTC')
),
License\Holder::fromString('Andreas Möller'),
License\Url::fromString('https://github.com/localheinz/localheinz.com')
);

$finder = Finder::create()->in(__DIR__);

return Config::create()
->setFinder($finder)
->setRules([
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $license->header(),
'location' => 'after_declare_strict',
'separate' => 'both',
],
]);
```

### GitHub Actions

When using [GitHub Actions](https://github.com/features/actions), you can set up a scheduled workflow that opens a pull request to the license year automatically on January 1st:
Expand Down Expand Up @@ -144,6 +194,7 @@ Note that pull requests opened or commits pushed by GitHub Actions will not trig
The following license types are currently available:

- [`Ergebnis\License\Type\MIT`](src/Type/MIT.php)
- [`Ergebnis\License\Type\None`](src/Type/None.php)

:bulb: Need a different license type? Feel free to open a pull request!

Expand Down
59 changes: 59 additions & 0 deletions src/Type/None.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\License\Type;

use Ergebnis\License\Header;
use Ergebnis\License\Holder;
use Ergebnis\License\Period;
use Ergebnis\License\Template;
use Ergebnis\License\Url;

final class None
{
private Header $header;

private function __construct(
Template $headerTemplate,
Period $period,
Holder $holder,
Url $url
) {
$header = Header::createWithoutReferenceToLicenseFile(
$headerTemplate,
$period,
$holder,
$url,
);

$this->header = $header;
}

public static function text(
Period $period,
Holder $holder,
Url $url
): self {
return new self(
Template::fromFile(__DIR__ . '/../../resource/header/without-reference-to-license-file.txt'),
$period,
$holder,
$url,
);
}

public function header(): string
{
return $this->header->toString();
}
}
66 changes: 66 additions & 0 deletions test/Unit/Type/NoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

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

namespace Ergebnis\License\Test\Unit\Type;

use Ergebnis\License\Holder;
use Ergebnis\License\Range;
use Ergebnis\License\Test;
use Ergebnis\License\Type;
use Ergebnis\License\Url;
use Ergebnis\License\Year;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\License\Type\None
*
* @uses \Ergebnis\License\Header
* @uses \Ergebnis\License\Holder
* @uses \Ergebnis\License\Range
* @uses \Ergebnis\License\Template
* @uses \Ergebnis\License\Url
* @uses \Ergebnis\License\Year
*/
final class NoneTest extends Framework\TestCase
{
use Test\Util\Helper;

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

$range = Range::since(
Year::fromString($faker->year()),
new \DateTimeZone($faker->timezone()),
);
$holder = Holder::fromString($faker->name());
$url = Url::fromString($faker->url());

$license = Type\None::text(
$range,
$holder,
$url,
);

$expected = <<<TXT
Copyright (c) {$range->toString()} {$holder->toString()}
@see {$url->toString()}
TXT;

self::assertSame($expected, $license->header());
}
}

0 comments on commit ed520d6

Please sign in to comment.