From 2c2028330bd5eaae83c5147e537e89494f76607c Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Sat, 6 Nov 2021 14:42:50 +0000 Subject: [PATCH] Add `include_author` option #30 --- README.md | 3 +- src/Composer/Extra/StraussConfig.php | 23 +++++++++ src/Licenser.php | 17 +++++-- .../Unit/Composer/Extra/StraussConfigTest.php | 47 +++++++++++++++++++ tests/Unit/LicenserTest.php | 47 ++++++++++++++++++- 5 files changed, 132 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 660e9dc5..8edd6944 100644 --- a/README.md +++ b/README.md @@ -96,12 +96,13 @@ The following configuration is inferred: - `classmap_prefix` defines the default string to prefix class names in the global namespace - `packages` is the list of packages to process. If absent, all packages in the `require` key of your `composer.json` are included - `classmap_output` is a `bool` to decide if Strauss will create `autoload-classmap.php` and `autoload.php`. If it is not set, it is `false` if `target_directory` is in your project's `autoload` key, `true` otherwise. -- `include_modified_date` is a `bool` to decide if Strauss should include a date in the (phpdoc) header written to modified files. Defaults to `true`. The following configuration is default: - `delete_vendor_files`: `false` a boolean flag to indicate if files copied from the packages' vendor directories should be deleted after being processed. It defaults to false, so any destructive change is opt-in. - `exclude_from_prefix` / [`file_patterns`](https://github.com/BrianHenryIE/strauss/blob/83484b79cfaa399bba55af0bf4569c24d6eb169d/src/ChangeEnumerator.php#L92-L96) : `[/psr.*/]` PSR namespaces are ignored by default for interoperability. If you override this key, be sure to include `/psr.*/` too. +- `include_modified_date` is a `bool` to decide if Strauss should include a date in the (phpdoc) header written to modified files. Defaults to `true`. +- `include_author` is a `bool` to decide if Strauss should include the author name in the (phpdoc) header written to modified files. Defaults to `true`. The remainder is empty: diff --git a/src/Composer/Extra/StraussConfig.php b/src/Composer/Extra/StraussConfig.php index 0eb3d2bd..ed1a0c43 100644 --- a/src/Composer/Extra/StraussConfig.php +++ b/src/Composer/Extra/StraussConfig.php @@ -104,6 +104,12 @@ class StraussConfig */ protected $includeModifiedDate = true; + /** + * Should the author name be included in the header for modified files? + * + * @var bool + */ + protected $includeAuthor = true; /** * Read any existing Mozart config. @@ -489,4 +495,21 @@ public function setIncludeModifiedDate(bool $includeModifiedDate): void { $this->includeModifiedDate = $includeModifiedDate; } + + + /** + * @return bool + */ + public function isIncludeAuthor(): bool + { + return $this->includeAuthor; + } + + /** + * @param bool $includeModifiedDate + */ + public function setIncludeAuthor(bool $includeAuthor): void + { + $this->includeAuthor = $includeAuthor; + } } diff --git a/src/Licenser.php b/src/Licenser.php index eb15ea63..f493ebc6 100644 --- a/src/Licenser.php +++ b/src/Licenser.php @@ -38,6 +38,12 @@ class Licenser protected bool $includeModifiedDate; + /** + * @see StraussConfig::isIncludeAuthor() + * @var bool + */ + protected bool $includeAuthor = true; + /** * An array of files relative to the project vendor folder. * @@ -64,6 +70,7 @@ public function __construct(StraussConfig $config, string $workingDir, array $de $this->targetDirectory = $config->getTargetDirectory(); $this->vendorDir = $config->getVendorDirectory(); $this->includeModifiedDate = $config->isIncludeModifiedDate(); + $this->includeAuthor = $config->isIncludeAuthor(); $this->filesystem = new Filesystem(new Local($workingDir)); } @@ -187,11 +194,15 @@ public function addChangeDeclarationToPhpString( $author = $this->author; $licenseDeclaration = "@license {$packageLicense}"; + $modifiedDeclaration = 'Modified '; + if ($this->includeAuthor) { + $modifiedDeclaration .= "by {$author} "; + } if ($this->includeModifiedDate) { - $modifiedDeclaration = "Modified by {$author} on {$modifiedDate} using Strauss."; - } else { - $modifiedDeclaration = "Modified by {$author} using Strauss."; + $modifiedDeclaration .= "on {$modifiedDate} "; } + $modifiedDeclaration .= 'using Strauss.'; + $straussLink = "@see https://github.com/BrianHenryIE/strauss"; // php-open followed by some whitespace and new line until the first ... diff --git a/tests/Unit/Composer/Extra/StraussConfigTest.php b/tests/Unit/Composer/Extra/StraussConfigTest.php index 2b850c14..bdc5099c 100644 --- a/tests/Unit/Composer/Extra/StraussConfigTest.php +++ b/tests/Unit/Composer/Extra/StraussConfigTest.php @@ -682,4 +682,51 @@ public function testIncludeModifiedDate() $this->assertFalse($sut->isIncludeModifiedDate()); } + + + public function testIncludeAuthorDefaultTrue() + { + + $composerExtraStraussJson = <<<'EOD' +{ + "extra":{ + "strauss": { + "namespace_prefix": "BrianHenryIE\\Strauss\\" + } + } +} +EOD; + $tmpfname = tempnam(sys_get_temp_dir(), 'strauss-test-'); + file_put_contents($tmpfname, $composerExtraStraussJson); + + $composer = Factory::create(new NullIO(), $tmpfname); + + $sut = new StraussConfig($composer); + + $this->assertTrue($sut->isIncludeAuthor()); + } + + + public function testIncludeAuthorFalse() + { + + $composerExtraStraussJson = <<<'EOD' +{ + "extra":{ + "strauss": { + "namespace_prefix": "BrianHenryIE\\Strauss\\", + "include_author": false + } + } +} +EOD; + $tmpfname = tempnam(sys_get_temp_dir(), 'strauss-test-'); + file_put_contents($tmpfname, $composerExtraStraussJson); + + $composer = Factory::create(new NullIO(), $tmpfname); + + $sut = new StraussConfig($composer); + + $this->assertFalse($sut->isIncludeAuthor()); + } } diff --git a/tests/Unit/LicenserTest.php b/tests/Unit/LicenserTest.php index be53efa8..e52d035b 100644 --- a/tests/Unit/LicenserTest.php +++ b/tests/Unit/LicenserTest.php @@ -83,6 +83,7 @@ public function testAppendHeaderCommentInformationNoHeader() $config = $this->createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); $sut = new Licenser($config, __DIR__, array(), $author); @@ -116,6 +117,7 @@ public function testAppendHeaderCommentInformationNoHeader() /** * Not including the date was reported as not working. + * The real problem was the master readme was ahead of the packagist release. * * @see https://github.com/BrianHenryIE/strauss/issues/35 * @@ -128,6 +130,7 @@ public function testAppendHeaderCommentNoDate() $config = $this->createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(false); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); $sut = new Licenser($config, __DIR__, array(), $author); @@ -146,6 +149,42 @@ public function testAppendHeaderCommentNoDate() * @see https://github.com/BrianHenryIE/strauss */ +namespace net\authorize\api\contract\v1; +EOD; + + $actual = $sut->addChangeDeclarationToPhpString($given, '25-April-2021', 'proprietary'); + + $this->assertEquals($expected, $actual); + } + + /** + * @covers ::addChangeDeclarationToPhpString + */ + public function testAppendHeaderCommentNoAuthor() + { + + $author = 'BrianHenryIE'; + + $config = $this->createMock(StraussConfig::class); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(false); + + $sut = new Licenser($config, __DIR__, array(), $author); + + $given = <<<'EOD' +createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true); - + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); + $author = 'BrianHenryIE'; $sut = new Licenser($config, __DIR__, array(), $author); @@ -215,6 +255,7 @@ public function testWithTwoCommentsBeforeFirstCode() $config = $this->createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); $author = 'BrianHenryIE'; $sut = new Licenser($config, __DIR__, array(), $author); @@ -278,6 +319,7 @@ public function testUnusualHeaderCommentStyle() $config = $this->createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); $author = 'BrianHenryIE'; $sut = new Licenser($config, __DIR__, array(), $author); @@ -325,6 +367,7 @@ public function testCommentWithLicenseWord() $config = $this->createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); $author = 'BrianHenryIE'; $sut = new Licenser($config, __DIR__, array(), $author); @@ -386,6 +429,7 @@ public function testIncorrectlyMatching() $config = $this->createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); $author = 'BrianHenryIE'; $sut = new Licenser($config, __DIR__, array(), $author); @@ -458,6 +502,7 @@ public function testLicenseDetailsOnlyInsertedOncePerFile() $config = $this->createMock(StraussConfig::class); $config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true); + $config->expects($this->once())->method('isIncludeAuthor')->willReturn(true); $author = 'BrianHenryIE'; $sut = new Licenser($config, __DIR__, array(), $author);