Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Fix PHP code style issues

on: [push]

permissions:
contents: write

jobs:
style:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Fix PHP code style issues
uses: aglipanci/laravel-pint-action@2.5
with:
preset: laravel

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Fix styling
23 changes: 0 additions & 23 deletions .github/workflows/php-cs-fixer.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
Expand All @@ -35,7 +35,7 @@ jobs:
coverage: none

- name: Install dependencies
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction

- name: Configure matchers
uses: mheap/phpunit-matcher-action@v1
Expand Down
40 changes: 0 additions & 40 deletions .php_cs.dist

This file was deleted.

2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"anahkiasen/underscore-php": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"phpunit/phpunit" : "^9.5"
},
"autoload": {
Expand All @@ -36,6 +37,7 @@
}
},
"scripts": {
"format": "vendor/bin/pint",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/ErrorCreatingString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Exception;

class ErrorCreatingString extends Exception
{
}
class ErrorCreatingString extends Exception {}
4 changes: 1 addition & 3 deletions src/Exceptions/UnknownFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Exception;

class UnknownFunction extends Exception
{
}
class UnknownFunction extends Exception {}
4 changes: 1 addition & 3 deletions src/Exceptions/UnsetOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Exception;

class UnsetOffset extends Exception
{
}
class UnsetOffset extends Exception {}
8 changes: 2 additions & 6 deletions src/Integrations/Underscore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Underscore
{
protected array $underscoreMethods =
[
//name, firstArgumentIsString, returnsAString
// name, firstArgumentIsString, returnsAString
'accord' => [false, true],
'random' => [false, true],
'quickRandom' => [false, true],
Expand Down Expand Up @@ -43,11 +43,7 @@ class Underscore
];

/**
* @param \Spatie\String\Str $string
* @param string $method
* @param array $args
*
* @return mixed|\Spatie\String\Str
* @return mixed|Str
*/
public function call(Str $string, string $method, array $args)
{
Expand Down
54 changes: 22 additions & 32 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use Spatie\String\Exceptions\UnknownFunction;
use Spatie\String\Exceptions\UnsetOffset;
use Spatie\String\Integrations\Underscore;
use Underscore\Methods\StringsMethods;

/**
* Magic methods provided by underscore are documented here.
*
* @see \Underscore\Methods\StringsMethods
* @see StringsMethods
*
* @method \Spatie\String\Str accord($count, $many, $one, $zero = null)
* @method \Spatie\String\Str random($length = 16)
Expand Down Expand Up @@ -72,10 +73,8 @@ public function __toString(): string
/**
* Get the string between the given start and end.
*
* @param $start
* @param $end
*
* @return \Spatie\String\Str
* @return Str
*/
public function between(string $start, string $end): static
{
Expand All @@ -84,11 +83,11 @@ public function between(string $start, string $end): static
}

if ($start != '' && ! str_contains($this->string, $start)) {
return new static();
return new static;
}

if ($end != '' && ! str_contains($this->string, $end)) {
return new static();
return new static;
}

if ($start == '') {
Expand Down Expand Up @@ -123,17 +122,15 @@ public function toLower(): static
* of the string is always a full word concatinated with the
* specified moreTextIndicator.
*
* @param int $length
* @param string $moreTextIndicator
*
* @return \Spatie\String\Str
* @return Str
*/
public function tease(int $length = 200, string $moreTextIndicator = '...'): static
{
$sanitizedString = $this->sanitizeForTease($this->string);

if (strlen($sanitizedString) === 0) {
return new static();
return new static;
}

if (strlen($sanitizedString) <= $length) {
Expand All @@ -150,18 +147,18 @@ private function sanitizeForTease(string $string): string
{
$string = trim($string);

//remove html
// remove html
$string = strip_tags($string);

//replace multiple spaces
// replace multiple spaces
$string = preg_replace("/\s+/", ' ', $string);

return $string;
}

public function replaceFirst(mixed $search, string $replace): static
{
if ((string)$search === '') {
if ((string) $search === '') {
return $this;
}

Expand All @@ -178,7 +175,7 @@ public function replaceFirst(mixed $search, string $replace): static

public function replaceLast(mixed $search, string $replace): static
{
if ((string)$search === '') {
if ((string) $search === '') {
return $this;
}

Expand All @@ -196,9 +193,8 @@ public function replaceLast(mixed $search, string $replace): static
/**
* Prefix a string.
*
* @param $string
*
* @return \Spatie\String\Str
* @return Str
*/
public function prefix($string): static
{
Expand All @@ -208,9 +204,8 @@ public function prefix($string): static
/**
* Suffix a string.
*
* @param $string
*
* @return \Spatie\String\Str
* @return Str
*/
public function suffix(mixed $string): static
{
Expand All @@ -225,12 +220,12 @@ public function concat(mixed $string): static
/**
* Get the possessive version of a string.
*
* @return \Spatie\String\Str
* @return Str
*/
public function possessive(): static
{
if ($this->string === '') {
return new static();
return new static;
}

$noApostropheEdgeCases = ['it'];
Expand All @@ -247,10 +242,8 @@ public function possessive(): static
* Returns an empty string when the offset doesn't exist.
* Use a negative index to start counting from the last element.
*
* @param string $delimiter
* @param int $index
*
* @return \Spatie\String\Str
* @return Str
*/
public function segment(string $delimiter, int $index): static
{
Expand Down Expand Up @@ -279,9 +272,8 @@ public function lastSegment(string $delimiter): static
/**
* Pop (remove) the last segment of a string based on a delimiter.
*
* @param string $delimiter
*
* @return \Spatie\String\Str
* @return Str
*/
public function pop(string $delimiter): static
{
Expand All @@ -293,24 +285,22 @@ public function trim(string $characterMask = " \t\n\r\0\x0B"): static
return new static(trim($this->string, $characterMask));
}

public function contains(array | string $needle, bool $caseSensitive = false, bool $absolute = false): bool
public function contains(array|string $needle, bool $caseSensitive = false, bool $absolute = false): bool
{
return $this->find($needle, $caseSensitive, $absolute);
}

/**
* Unknown methods calls will be handled by various integrations.
*
* @param $method
* @param $args
*
* @return mixed|\Spatie\String\Str
*@throws UnknownFunction
* @return mixed|Str
*
* @throws UnknownFunction
*/
public function __call($method, $args)
{
$underscore = new Underscore();
$underscore = new Underscore;

if ($underscore->isSupportedMethod($method)) {
return $underscore->call($this, $method, $args);
Expand Down Expand Up @@ -338,6 +328,6 @@ public function offsetSet($offset, $value)

public function offsetUnset($offset)
{
throw new UnsetOffset();
throw new UnsetOffset;
}
}
7 changes: 4 additions & 3 deletions tests/Functions/BetweenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\String\Test\Functions;

use PHPUnit\Framework\TestCase;
use Spatie\String\Str;

class BetweenTest extends TestCase
{
Expand Down Expand Up @@ -37,13 +38,13 @@ public function it_returns_an_the_middle_even_if_start_and_end_are_the_same_stri
}

/** @test */
public function it_returns_everything_after_the_start_if_end_is_an_emptyString()
public function it_returns_everything_after_the_start_if_end_is_an_empty_string()
{
$this->assertEquals('MiddleEnd', (string) string('StartMiddleEnd')->between('Start', ''));
}

/** @test */
public function it_returns_everything_until_the_end_if_start_is_an_emptyString()
public function it_returns_everything_until_the_end_if_start_is_an_empty_string()
{
$this->assertEquals('StartMiddle', (string) string('StartMiddleEnd')->between('', 'End'));
}
Expand All @@ -57,6 +58,6 @@ public function it_returns_everything_if_both_start_and_end_are_empty()
/** @test */
public function it_is_chainable()
{
$this->assertInstanceOf(\Spatie\String\Str::class, string('test')->between('', ''));
$this->assertInstanceOf(Str::class, string('test')->between('', ''));
}
}
Loading
Loading