Skip to content

Commit

Permalink
Add include_author option #30
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Nov 6, 2021
1 parent e64eb70 commit 2c20283
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
23 changes: 23 additions & 0 deletions src/Composer/Extra/StraussConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
17 changes: 14 additions & 3 deletions src/Licenser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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));
}
Expand Down Expand Up @@ -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 ...
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Composer/Extra/StraussConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
47 changes: 46 additions & 1 deletion tests/Unit/LicenserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
*
Expand All @@ -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);

Expand All @@ -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'
<?php
namespace net\authorize\api\contract\v1;
EOD;

$expected = <<<'EOD'
<?php
/**
* @license proprietary
*
* Modified using Strauss.
* @see https://github.com/BrianHenryIE/strauss
*/
namespace net\authorize\api\contract\v1;
EOD;

Expand All @@ -162,7 +201,8 @@ public function testWithLicenceAlreadyInHeader(): void

$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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2c20283

Please sign in to comment.