Skip to content

Commit

Permalink
ignore whitespace feature
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Jan 26, 2025
1 parent 818f688 commit 2e10be1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Php Smart Search/replace Functionality
[![tests](https://github.com/imanghafoori1/php-smart-search-replace/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/imanghafoori1/php-smart-search-replace/actions/workflows/tests.yml)
[![Coverage Status](https://coveralls.io/repos/github/imanghafoori1/php-smart-search-replace/badge.svg?branch=main)](https://coveralls.io/github/imanghafoori1/php-smart-search-replace?branch=main)
[![Total Downloads](https://img.shields.io/packagist/dt/imanghafoori/php-search-replace.svg?style=flat-square)](https://packagist.org/packages/imanghafoori/php-search-replace)
[![Latest Stable Version](https://poser.pugx.org/imanghafoori/php-search-replace/v/stable?format=flat-square)](https://packagist.org/packages/imanghafoori/php-search-replace)
[![Check Imports](https://github.com/imanghafoori1/php-smart-search-replace/actions/workflows/imports.yml/badge.svg?branch=main)](https://github.com/imanghafoori1/php-smart-search-replace/actions/workflows/imports.yml)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Check Imports](https://github.com/imanghafoori1/php-smart-search-replace/actions/workflows/imports.yml/badge.svg?branch=main)](https://github.com/imanghafoori1/php-smart-search-replace/actions/workflows/imports.yml)


## It is much easier than using regex.
Expand Down Expand Up @@ -49,6 +48,7 @@ Here is a copmerehensive list of placeholders you can use:
- `<statement>`: to capture a whole php statement.
- `"<name:nam1,nam2>"` or `<name>`: for method or function names. `->where` or `::where`
- `<white_space>`: for whitespace blocks
- `<not_whitespace>`: for non-whitespace
- `<bool>` or `'<boolean>'`: for true or false (acts case-insensetive)
- `<number>`: for numeric values
- `<cast>`: for type-casts like: `(array) $a;`
Expand Down
23 changes: 14 additions & 9 deletions src/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Imanghafoori\SearchReplace;

use Imanghafoori\SearchReplace\Keywords;

class Finder
{
public static $primitiveTokens = [
Expand Down Expand Up @@ -31,6 +29,7 @@ class Finder
Keywords\Name::class,
Keywords\DocBlock::class,
Keywords\WhiteSpace::class,
Keywords\BlackSpace::class,
Keywords\Comment::class,
Keywords\MethodVisibility::class,
Keywords\Boolean::class,
Expand All @@ -50,7 +49,7 @@ class Finder
//',' => ',',
];

public static function compareTokens($pattern, $tokens, $startFrom, $namedPatterns = [])
public static function compareTokens($pattern, $tokens, $startFrom, $namedPatterns = [], $ignoreWhitespace = true)
{
$pi = $j = 0;
$tCount = count($tokens);
Expand All @@ -73,10 +72,15 @@ public static function compareTokens($pattern, $tokens, $startFrom, $namedPatter
return false;
}

[$pToken, $j] = self::getNextToken($pattern, $j);
[$pToken, $j] = self::getNextToken($pattern, $j, $ignoreWhitespace ? null : T_WHITESPACE);

$pi = $startFrom;
[, $startFrom] = self::forwardToNextToken($pToken, $tokens, $startFrom);

if ($ignoreWhitespace) {
[, $startFrom] = self::forwardToNextToken($pToken, $tokens, $startFrom);
} else {
[, $startFrom] = self::getNextToken($tokens, $startFrom, T_WHITESPACE);
}
}

if ($pCount === $j) {
Expand Down Expand Up @@ -187,7 +191,8 @@ public static function getMatches(
$namedPatterns = [],
$filters = [],
$startFrom = 1,
$maxMatch = null
$maxMatch = null,
$ignoreWhitespace = true
) {
$pIndex = PatternParser::firstNonOptionalPlaceholder($patternTokens);
$optionalStartingTokens = array_slice($patternTokens, 0, $pIndex);
Expand All @@ -199,7 +204,7 @@ public static function getMatches(

while ($i < $allCount) {
$restPatternTokens = array_slice($patternTokens, $pIndex);
$isMatch = self::compareTokens($restPatternTokens, $tokens, $i, $namedPatterns);
$isMatch = self::compareTokens($restPatternTokens, $tokens, $i, $namedPatterns, $ignoreWhitespace);
if (! $isMatch) {
$i++;
continue;
Expand Down Expand Up @@ -243,7 +248,7 @@ public static function compareIt($tToken, int $type, $token, &$i)

private static function forwardToNextToken($pToken, $tokens, $startFrom)
{
if (isset($pToken[1]) && self::is($pToken, '<white_space>')) {
if (isset($pToken[1]) && (self::is($pToken, ['<white_space>', '<not_whitespace>']))) {
return self::getNextToken($tokens, $startFrom, T_WHITESPACE);
} elseif (isset($pToken[1]) && self::is($pToken, '<comment>')) {
return self::getNextToken($tokens, $startFrom, T_COMMENT);
Expand Down Expand Up @@ -295,7 +300,7 @@ private static function optionalStartingTokens($optionalStartingTokens, $tokens,
public static function extractValue($matches, $first = '')
{
$segments = [$first];

foreach ($matches as $match) {
$segments[] = $match[0][1];
}
Expand Down
22 changes: 22 additions & 0 deletions src/Keywords/BlackSpace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Imanghafoori\SearchReplace\Keywords;

class BlackSpace
{
public static function is($string)
{
return $string === '<not_whitespace>';
}

public static function getValue($tokens, &$startFrom, &$placeholderValues, $pToken)
{
$t = $tokens[$startFrom];

if ($t[0] === T_WHITESPACE) {
return false;
}

$placeholderValues[] = $t;
}
}
12 changes: 7 additions & 5 deletions src/PatternParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private static function getParams($pToken, $id)
public static function parsePatterns($patterns, $normalize = true)
{
$defaults = [
'ignore_whitespaces' => true,
'predicate' => null,
'mutator' => null,
'named_patterns' => [],
Expand Down Expand Up @@ -50,7 +51,7 @@ public static function tokenize($pattern)

private static function cleanComments($pattern)
{
foreach (['"', "'", ''] as $c) {
foreach (['"', "'"] as $c) {
for ($i = 1; $i !== 11; $i++) {
$pattern = str_replace("$c<$i:", "$c<", $pattern, $count);
}
Expand Down Expand Up @@ -133,6 +134,7 @@ private static function getSearchPatterns()
{
$names = implode(',', [
'white_space',
'not_whitespace',
'string',
'str',
'variable',
Expand All @@ -156,10 +158,10 @@ private static function getSearchPatterns()
'boolean',
]);

// the order of the patterns matter.
return [
['search' => '<"<name:'.$names.'>">?', 'replace' => '"<"<1>">?"'],
['search' => '<"<name:'.$names.'>">', 'replace' => '"<"<1>">"'],
// the order of the patterns matter.
['search' => '<"<name:'.$names.'>">?', 'replace' => '"<"<1>">?"',],
['search' => '<"<name:'.$names.'>">', 'replace' => '"<"<1>">"',],
];
}

Expand All @@ -173,7 +175,7 @@ private static function addQuotes(string $search, array $all)

private static function normalize($to, $all)
{
$to['search'] = self::addQuotes(self::cleanComments($to['search']), $all);
$to['search'] = self::addQuotes($to['search'], $all);

is_string($to['replace'] ?? 0) && ($to['replace'] = self::addQuotes(
$to['replace'],
Expand Down
3 changes: 2 additions & 1 deletion src/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static function searchReplaceOnePattern($pattern, $tokens, $maxMatches =
$pattern['named_patterns'],
$pattern['filters'],
1,
$maxMatches
$maxMatches,
$pattern['ignore_whitespaces']
);

[
Expand Down

0 comments on commit 2e10be1

Please sign in to comment.