Skip to content

Commit

Permalink
Merge pull request #23 from peter-gribanov/text_seek
Browse files Browse the repository at this point in the history
Not use dangerous method \ArrayIterator::seek()
  • Loading branch information
peter-gribanov authored Aug 20, 2019
2 parents 86588cf + 02b6856 commit 77f7446
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
18 changes: 4 additions & 14 deletions AnalyzerText/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,24 @@ protected function replace(Word $word)
/**
* Возвращает предыдущее слово.
*
* @param int|null $shift Смещение
* @param int $shift Смещение
*
* @return Word|null
*/
protected function getPreviousWord($shift = 1)
{
return $this->getNextWord($shift * -1);
return $this->getText()->offsetGet($this->getText()->key() + $shift * -1);
}

/**
* Возвращает следующее слово.
*
* @param int|null $shift Смещение
* @param int $shift Смещение
*
* @return Word|null
*/
protected function getNextWord($shift = 1)
{
$position = $this->getText()->key();

try {
$this->getText()->seek($position + $shift);
} catch (\OutOfBoundsException $e) {
return null;
}
$word = $this->getText()->current();
$this->getText()->seek($position);

return $word;
return $this->getText()->offsetGet($this->getText()->key() + $shift);
}
}
11 changes: 5 additions & 6 deletions AnalyzerText/Filter/WordList/WordList.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,19 @@ public function isSequence(Word $word)
{
$plain = $word->getPlain();
foreach ($this->sequence as $sequence) {
if ($sequence[0] == $plain) {
if ($sequence[0] === $plain) {
$sequence_length = count($sequence);
for ($i = 1; $i < $sequence_length; ++$i) {
if (!($word = $this->getNextWord($i)) || $word->getPlain() != $sequence[$i]) {
return false;
if (!($next_word = $this->getNextWord($i)) || $next_word->getPlain() != $sequence[$i]) {
continue 2;
}
}

// удаляем слова из последовательности
$key = $this->getText()->key();
for ($i = 1; $i < $sequence_length; ++$i) {
$this->getText()->seek($key + $i);
$this->getText()->remove();
$this->getText()->offsetUnset($key + $i);
}
$this->getText()->seek($key);

return true;
}
Expand Down
14 changes: 12 additions & 2 deletions AnalyzerText/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ public function getWords()
/**
* Возвращает текущий элемент
*
* @return Word
* @return Word|null
*/
public function current()
{
return new Word(parent::current(), $this->plains[$this->key()]);
return parent::current() ? new Word(parent::current(), $this->plains[$this->key()]) : null;
}

/**
* @param int $index
*
* @return Word|null
*/
public function offsetGet($index)
{
return $this->offsetExists($index) ? new Word(parent::offsetGet($index), $this->plains[$index]) : null;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/Filter/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public function getInformativeText()
array('Я буду Через час или два', 'буду час два'),
array('аКкуратно распИлить', 'распИлить'),
array('Аааа-а-а сказал Василичь', 'сказал Василичь'),
array('со всей силы', ''),
array('Петрович ударил со всей силы кулаком по столу', 'Петрович ударил кулаком столу'),
array(
'Шел дождь и в то же время снег. И тот и другой были крайне неприятными',
'Шел дождь снег были неприятными',
),
);
}

Expand Down

0 comments on commit 77f7446

Please sign in to comment.