Skip to content

Commit 9d1641c

Browse files
committed
Moved word preprocessing and morphy methods info into separate traits
1 parent 4345b05 commit 9d1641c

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

src/Morphy.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InvalidArgumentException;
66

7+
use SEOService2020\Morphy\Traits\PreprocessWord;
78
use phpMorphy;
89
use phpMorphy_GrammemsProvider_GrammemsProviderInterface;
910

@@ -68,45 +69,45 @@ public function name(): string {
6869
}
6970

7071
public function findWord($word, $type = self::NORMAL) {
71-
return parent::findWord(self::preprocessedWord($word, $this), $type);
72+
return parent::findWord($this->preprocessedWord($word), $type);
7273
}
7374

7475
public function getBaseForm($word, $type = self::NORMAL) {
75-
return parent::getBaseForm(self::preprocessedWord($word, $this), $type);
76+
return parent::getBaseForm($this->preprocessedWord($word), $type);
7677
}
7778

7879
public function getAllForms($word, $type = self::NORMAL) {
79-
return parent::getAllForms(self::preprocessedWord($word, $this), $type);
80+
return parent::getAllForms($this->preprocessedWord($word), $type);
8081
}
8182

8283
public function getPseudoRoot($word, $type = self::NORMAL) {
83-
return parent::getPseudoRoot(self::preprocessedWord($word, $this), $type);
84+
return parent::getPseudoRoot($this->preprocessedWord($word), $type);
8485
}
8586

8687
public function getPartOfSpeech($word, $type = self::NORMAL) {
87-
return parent::getPartOfSpeech(self::preprocessedWord($word, $this), $type);
88+
return parent::getPartOfSpeech($this->preprocessedWord($word), $type);
8889
}
8990

9091
public function getAllFormsWithAncodes($word, $type = self::NORMAL) {
91-
return parent::getAllFormsWithAncodes(self::preprocessedWord($word, $this), $type);
92+
return parent::getAllFormsWithAncodes($this->preprocessedWord($word), $type);
9293
}
9394

9495
public function getAllFormsWithGramInfo($word, $asText = true, $type = self::NORMAL) {
9596
return parent::getAllFormsWithGramInfo(
96-
self::preprocessedWord($word, $this), $asText, $type
97+
$this->preprocessedWord($word), $asText, $type
9798
);
9899
}
99100

100101
public function getAncode($word, $type = self::NORMAL) {
101-
return parent::getAncode(self::preprocessedWord($word, $this), $type);
102+
return parent::getAncode($this->preprocessedWord($word), $type);
102103
}
103104

104105
public function getGramInfo($word, $type = self::NORMAL) {
105-
return parent::getGramInfo(self::preprocessedWord($word, $this), $type);
106+
return parent::getGramInfo($this->preprocessedWord($word), $type);
106107
}
107108

108109
public function getGramInfoMergeForms($word, $type = self::NORMAL) {
109-
return parent::getGramInfoMergeForms(self::preprocessedWord($word, $this), $type);
110+
return parent::getGramInfoMergeForms($this->preprocessedWord($word), $type);
110111
}
111112

112113
public function castFormByAncode(
@@ -118,9 +119,9 @@ public function castFormByAncode(
118119
$type = self::NORMAL
119120
) {
120121
return parent::castFormByAncode(
121-
self::preprocessedWord($word, $this),
122-
self::preprocessedWord($ancode, $this),
123-
self::preprocessedWord($commonAncode, $this),
122+
$this->preprocessedWord($word),
123+
$this->preprocessedWord($ancode),
124+
$this->preprocessedWord($commonAncode),
124125
$returnOnlyWord,
125126
$callback,
126127
$type
@@ -136,8 +137,8 @@ public function castFormByGramInfo(
136137
$type = self::NORMAL
137138
) {
138139
return parent::castFormByGramInfo(
139-
self::preprocessedWord($word, $this),
140-
self::preprocessedWord($partOfSpeech, $this),
140+
$this->preprocessedWord($word),
141+
$this->preprocessedWord($partOfSpeech),
141142
$grammems,
142143
$returnOnlyWord,
143144
$callback,
@@ -154,8 +155,8 @@ public function castFormByPattern(
154155
$type = self::NORMAL
155156
) {
156157
return parent::castFormByPattern(
157-
self::preprocessedWord($word, $this),
158-
self::preprocessedWord($patternWord, $this),
158+
$this->preprocessedWord($word),
159+
$this->preprocessedWord($patternWord),
159160
$grammemsProvider,
160161
$returnOnlyWord,
161162
$callback,

src/MorphyManager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace SEOService2020\Morphy;
44

5-
use ReflectionMethod;
65
use BadMethodCallException;
76
use InvalidArgumentException;
87

8+
use SEOService2020\Morphy\Traits\MorphyMethodsInfo;
99
use phpMorphy;
1010

1111

1212
class MorphyManager
1313
{
14+
use MorphyMethodsInfo;
15+
1416
protected $morphies;
1517

1618
public function __construct(array $morphies)
@@ -39,7 +41,7 @@ public function morphy(string $name): ?Morphy
3941

4042
public function __call($name, $arguments)
4143
{
42-
if ((new ReflectionMethod(phpMorphy::class, $name))->isStatic()) {
44+
if (MorphyMethodsInfo::isStatic($name)) {
4345
return self::__callStatic($name, $arguments);
4446
}
4547

src/PreprocessWord.php renamed to src/Traits/MorphyMethodsInfo.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
<?php
22

3-
namespace SEOService2020\Morphy;
3+
namespace SEOService2020\Morphy\Traits;
44

5-
use Illuminate\Support\Str;
5+
use ReflectionMethod;
66

7+
use phpMorphy;
78

8-
trait PreprocessWord
9+
10+
trait MorphyMethodsInfo
911
{
10-
protected static function preprocessedWord(?string $word, Morphy $morphy): ?string
12+
public static function isStatic(string $methodName): bool
1113
{
12-
if ($word === null) {
13-
return null;
14-
}
15-
16-
return $morphy->isInUpperCase() ? Str::upper(trim($word)) : Str::lower(trim($word));
14+
return (new ReflectionMethod(phpMorphy::class, $methodName))->isStatic();
1715
}
1816

1917
public static function takesWordParameter(string $methodName): bool

src/Traits/PreprocessWord.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace SEOService2020\Morphy\Traits;
4+
5+
use Illuminate\Support\Str;
6+
7+
8+
trait PreprocessWord
9+
{
10+
protected function preprocessedWord(?string $word): ?string
11+
{
12+
if ($word === null) {
13+
return null;
14+
}
15+
16+
return $this->isInUpperCase() ? Str::upper(trim($word)) : Str::lower(trim($word));
17+
}
18+
}

0 commit comments

Comments
 (0)