Skip to content

Commit

Permalink
Fix possible key overlap in cache of Str::snake (#13943)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlakoff authored and taylorotwell committed Jun 11, 2016
1 parent bb59455 commit 4c4313c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ public static function slug($title, $separator = '-')
*/
public static function snake($value, $delimiter = '_')
{
$key = $value.$delimiter;
$key = $value;

if (isset(static::$snakeCache[$key])) {
return static::$snakeCache[$key];
if (isset(static::$snakeCache[$key][$delimiter])) {
return static::$snakeCache[$key][$delimiter];
}

if (! ctype_lower($value)) {
Expand All @@ -377,7 +377,7 @@ public static function snake($value, $delimiter = '_')
$value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
}

return static::$snakeCache[$key] = $value;
return static::$snakeCache[$key][$delimiter] = $value;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public function testSnake()
$this->assertEquals('laravel php framework', Str::snake('LaravelPhpFramework', ' '));
$this->assertEquals('laravel_php_framework', Str::snake('Laravel Php Framework'));
$this->assertEquals('laravel_php_framework', Str::snake('Laravel Php Framework '));
// ensure cache keys don't overlap
$this->assertEquals('laravel__php__framework', Str::snake('LaravelPhpFramework', '__'));
$this->assertEquals('laravel_php_framework_', Str::snake('LaravelPhpFramework_', '_'));
}

public function testStudly()
Expand Down

0 comments on commit 4c4313c

Please sign in to comment.