Skip to content

Commit 2c20283

Browse files
committed
Add include_author option coenjacobs#30
1 parent e64eb70 commit 2c20283

File tree

5 files changed

+132
-5
lines changed

5 files changed

+132
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ The following configuration is inferred:
9696
- `classmap_prefix` defines the default string to prefix class names in the global namespace
9797
- `packages` is the list of packages to process. If absent, all packages in the `require` key of your `composer.json` are included
9898
- `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.
99-
- `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`.
10099

101100
The following configuration is default:
102101

103102
- `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.
104103
- `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.
104+
- `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`.
105+
- `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`.
105106

106107
The remainder is empty:
107108

src/Composer/Extra/StraussConfig.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ class StraussConfig
104104
*/
105105
protected $includeModifiedDate = true;
106106

107+
/**
108+
* Should the author name be included in the header for modified files?
109+
*
110+
* @var bool
111+
*/
112+
protected $includeAuthor = true;
107113

108114
/**
109115
* Read any existing Mozart config.
@@ -489,4 +495,21 @@ public function setIncludeModifiedDate(bool $includeModifiedDate): void
489495
{
490496
$this->includeModifiedDate = $includeModifiedDate;
491497
}
498+
499+
500+
/**
501+
* @return bool
502+
*/
503+
public function isIncludeAuthor(): bool
504+
{
505+
return $this->includeAuthor;
506+
}
507+
508+
/**
509+
* @param bool $includeModifiedDate
510+
*/
511+
public function setIncludeAuthor(bool $includeAuthor): void
512+
{
513+
$this->includeAuthor = $includeAuthor;
514+
}
492515
}

src/Licenser.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class Licenser
3838

3939
protected bool $includeModifiedDate;
4040

41+
/**
42+
* @see StraussConfig::isIncludeAuthor()
43+
* @var bool
44+
*/
45+
protected bool $includeAuthor = true;
46+
4147
/**
4248
* An array of files relative to the project vendor folder.
4349
*
@@ -64,6 +70,7 @@ public function __construct(StraussConfig $config, string $workingDir, array $de
6470
$this->targetDirectory = $config->getTargetDirectory();
6571
$this->vendorDir = $config->getVendorDirectory();
6672
$this->includeModifiedDate = $config->isIncludeModifiedDate();
73+
$this->includeAuthor = $config->isIncludeAuthor();
6774

6875
$this->filesystem = new Filesystem(new Local($workingDir));
6976
}
@@ -187,11 +194,15 @@ public function addChangeDeclarationToPhpString(
187194
$author = $this->author;
188195

189196
$licenseDeclaration = "@license {$packageLicense}";
197+
$modifiedDeclaration = 'Modified ';
198+
if ($this->includeAuthor) {
199+
$modifiedDeclaration .= "by {$author} ";
200+
}
190201
if ($this->includeModifiedDate) {
191-
$modifiedDeclaration = "Modified by {$author} on {$modifiedDate} using Strauss.";
192-
} else {
193-
$modifiedDeclaration = "Modified by {$author} using Strauss.";
202+
$modifiedDeclaration .= "on {$modifiedDate} ";
194203
}
204+
$modifiedDeclaration .= 'using Strauss.';
205+
195206
$straussLink = "@see https://github.com/BrianHenryIE/strauss";
196207

197208
// php-open followed by some whitespace and new line until the first ...

tests/Unit/Composer/Extra/StraussConfigTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,51 @@ public function testIncludeModifiedDate()
682682

683683
$this->assertFalse($sut->isIncludeModifiedDate());
684684
}
685+
686+
687+
public function testIncludeAuthorDefaultTrue()
688+
{
689+
690+
$composerExtraStraussJson = <<<'EOD'
691+
{
692+
"extra":{
693+
"strauss": {
694+
"namespace_prefix": "BrianHenryIE\\Strauss\\"
695+
}
696+
}
697+
}
698+
EOD;
699+
$tmpfname = tempnam(sys_get_temp_dir(), 'strauss-test-');
700+
file_put_contents($tmpfname, $composerExtraStraussJson);
701+
702+
$composer = Factory::create(new NullIO(), $tmpfname);
703+
704+
$sut = new StraussConfig($composer);
705+
706+
$this->assertTrue($sut->isIncludeAuthor());
707+
}
708+
709+
710+
public function testIncludeAuthorFalse()
711+
{
712+
713+
$composerExtraStraussJson = <<<'EOD'
714+
{
715+
"extra":{
716+
"strauss": {
717+
"namespace_prefix": "BrianHenryIE\\Strauss\\",
718+
"include_author": false
719+
}
720+
}
721+
}
722+
EOD;
723+
$tmpfname = tempnam(sys_get_temp_dir(), 'strauss-test-');
724+
file_put_contents($tmpfname, $composerExtraStraussJson);
725+
726+
$composer = Factory::create(new NullIO(), $tmpfname);
727+
728+
$sut = new StraussConfig($composer);
729+
730+
$this->assertFalse($sut->isIncludeAuthor());
731+
}
685732
}

tests/Unit/LicenserTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function testAppendHeaderCommentInformationNoHeader()
8383

8484
$config = $this->createMock(StraussConfig::class);
8585
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true);
86+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
8687

8788
$sut = new Licenser($config, __DIR__, array(), $author);
8889

@@ -116,6 +117,7 @@ public function testAppendHeaderCommentInformationNoHeader()
116117

117118
/**
118119
* Not including the date was reported as not working.
120+
* The real problem was the master readme was ahead of the packagist release.
119121
*
120122
* @see https://github.com/BrianHenryIE/strauss/issues/35
121123
*
@@ -128,6 +130,7 @@ public function testAppendHeaderCommentNoDate()
128130

129131
$config = $this->createMock(StraussConfig::class);
130132
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(false);
133+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
131134

132135
$sut = new Licenser($config, __DIR__, array(), $author);
133136

@@ -146,6 +149,42 @@ public function testAppendHeaderCommentNoDate()
146149
* @see https://github.com/BrianHenryIE/strauss
147150
*/
148151
152+
namespace net\authorize\api\contract\v1;
153+
EOD;
154+
155+
$actual = $sut->addChangeDeclarationToPhpString($given, '25-April-2021', 'proprietary');
156+
157+
$this->assertEquals($expected, $actual);
158+
}
159+
160+
/**
161+
* @covers ::addChangeDeclarationToPhpString
162+
*/
163+
public function testAppendHeaderCommentNoAuthor()
164+
{
165+
166+
$author = 'BrianHenryIE';
167+
168+
$config = $this->createMock(StraussConfig::class);
169+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(false);
170+
171+
$sut = new Licenser($config, __DIR__, array(), $author);
172+
173+
$given = <<<'EOD'
174+
<?php
175+
176+
namespace net\authorize\api\contract\v1;
177+
EOD;
178+
179+
$expected = <<<'EOD'
180+
<?php
181+
/**
182+
* @license proprietary
183+
*
184+
* Modified using Strauss.
185+
* @see https://github.com/BrianHenryIE/strauss
186+
*/
187+
149188
namespace net\authorize\api\contract\v1;
150189
EOD;
151190

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

163202
$config = $this->createMock(StraussConfig::class);
164203
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true);
165-
204+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
205+
166206
$author = 'BrianHenryIE';
167207
$sut = new Licenser($config, __DIR__, array(), $author);
168208

@@ -215,6 +255,7 @@ public function testWithTwoCommentsBeforeFirstCode()
215255

216256
$config = $this->createMock(StraussConfig::class);
217257
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true);
258+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
218259

219260
$author = 'BrianHenryIE';
220261
$sut = new Licenser($config, __DIR__, array(), $author);
@@ -278,6 +319,7 @@ public function testUnusualHeaderCommentStyle()
278319

279320
$config = $this->createMock(StraussConfig::class);
280321
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true);
322+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
281323

282324
$author = 'BrianHenryIE';
283325
$sut = new Licenser($config, __DIR__, array(), $author);
@@ -325,6 +367,7 @@ public function testCommentWithLicenseWord()
325367

326368
$config = $this->createMock(StraussConfig::class);
327369
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true);
370+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
328371

329372
$author = 'BrianHenryIE';
330373
$sut = new Licenser($config, __DIR__, array(), $author);
@@ -386,6 +429,7 @@ public function testIncorrectlyMatching()
386429

387430
$config = $this->createMock(StraussConfig::class);
388431
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true);
432+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
389433

390434
$author = 'BrianHenryIE';
391435
$sut = new Licenser($config, __DIR__, array(), $author);
@@ -458,6 +502,7 @@ public function testLicenseDetailsOnlyInsertedOncePerFile()
458502

459503
$config = $this->createMock(StraussConfig::class);
460504
$config->expects($this->once())->method('isIncludeModifiedDate')->willReturn(true);
505+
$config->expects($this->once())->method('isIncludeAuthor')->willReturn(true);
461506

462507
$author = 'BrianHenryIE';
463508
$sut = new Licenser($config, __DIR__, array(), $author);

0 commit comments

Comments
 (0)