Skip to content

Commit

Permalink
Introduces Str::chop*
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Jun 26, 2024
1 parent 72c872e commit 933e3b3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ protected function sortImports($stub)
*/
protected function getNameInput()
{
return (string) Str::of($this->argument('name'))->trim()->beforeLast('.php');
return (string) Str::of($this->argument('name'))->trim()->chopEnd('.php');
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,42 @@ public static function charAt($subject, $index)
return mb_substr($subject, $index, 1);
}

/**
* Remove the given string(s) if it exists at the end of the haystack.
*
* @param string $subject
* @param string|array $needle
* @return string
*/
public static function chopEnd($subject, $needles)
{
foreach ((array) $needles as $needle) {
if (str_ends_with($subject, $needle)) {
return substr($subject, 0, -strlen($needle));
}
}

return $subject;
}

/**
* Remove the given string(s) if it exists at the start of the haystack.
*
* @param string $subject
* @param string|array $needle
* @return string
*/
public static function chopStart($subject, $needle)
{
foreach ((array) $needles as $needle) {

Check failure on line 272 in src/Illuminate/Support/Str.php

View workflow job for this annotation

GitHub Actions / Source Code

Undefined variable: $needles
if (str_starts_with($subject, $needle)) {
return substr($subject, strlen($needle));
}
}

return $subject;
}

/**
* Determine if a given string contains a given substring.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,28 @@ public function charAt($index)
return Str::charAt($this->value, $index);
}

/**
* Remove the given string(s) if it exists at the end of the haystack.
*
* @param string|array $needle
* @return static
*/
public function chopEnd($needles)
{
return new static(Str::chopEnd($this->value, $needles));
}

/**
* Remove the given string(s) if it exists at the start of the haystack.
*
* @param string|array $needle
* @return static
*/
public function chopStart($subject, $needle)
{
return new static(Str::chopStart($this->value, $needles));

Check failure on line 133 in src/Illuminate/Support/Stringable.php

View workflow job for this annotation

GitHub Actions / Source Code

Undefined variable: $needles
}

/**
* Get the basename of the class path.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/Console/GeneratorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ public function testItChopsPhpExtension()
], 'app/Console/Commands/FooCommand.php');
}

public function testItDoesNotTrimIndividualCharacters()
{
$this->artisan('make:command', ['name' => 'Photograph'])
->assertExitCode(0);

$this->assertFilenameExists('app/Console/Commands/Photograph.php');

$this->assertFileContains([
'class Photograph extends Command',
], 'app/Console/Commands/Photograph.php');
}

public function testItDoesNotChopCharactersWithinString()
{
$this->artisan('make:command', ['name' => 'Bar.php/Photograph'])
->assertExitCode(0);

$this->assertFilenameExists('app/Console/Commands/Bar.php/Photograph.php');

$this->assertFileContains([
'class Photograph extends Command',
], 'app/Console/Commands/Bar.php/Photograph.php');
}

public function testItChopsPhpExtensionFromMakeViewCommands()
{
$this->artisan('make:view', ['name' => 'foo.php'])
Expand Down

0 comments on commit 933e3b3

Please sign in to comment.