Article::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 15
ccs 11
cts 13
cp 0.8462
crap 3.0327
rs 9.9
c 0
b 0
f 0
1
<?php
2
namespace Mystem;
3
4
/**
5
 * Class MystemArticle
6
 */
7
class Article
8
{
9
    /* @var string $article */
10
    public $article = '';
11
12
    /* @var ArticleWord[] $words */
13
    public $words = array();
14
15
    /**
16
     * @param string $text
17
     */
18 4
    public function __construct($text)
19
    {
20 4
        $offset = 0;
21 4
        $this->article = $text;
22
23 4
        $stemmed = Mystem::stemm($text);
24 4
        foreach ($stemmed as $part) {
25 4
            $word = ArticleWord::newFromLexicalString($part, 1, $this->article);
26 4
            $position = @mb_strpos($this->article, $word->original, $offset);
27 4
            if ($position === false) { //Can't find original word
28
                $position = $offset + 1;
29
            }
30 4
            $word->position = $position;
31 4
            $offset = $word->position + mb_strlen($word->original);
32 4
            $this->words[] = $word;
33 4
        }
34 4
    }
35
36
    /**
37
     * @return string
38
     */
39 1
    public function getArticle()
40
    {
41 1
        return $this->article;
42
    }
43
44
    /**
45
     * @param bool $stopOnFirst
46
     * @return array
47
     */
48 2
    public function checkBadWords($stopOnFirst = false)
49
    {
50 2
        $result = array();
51 2
        foreach ($this->words as &$word) {
52 2
            if ($word->isBadWord()) {
53 1
                $result[$word->original] = $word->normalized();
54 1
                if ($stopOnFirst) {
55 1
                    break;
56
                }
57 1
            }
58 2
        }
59 2
        return $result;
60
    }
61
}
62