Completed
Push — master ( 8578af...928a6e )
by Владислав
03:06
created

DeCaptchaWikiMain   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 197
Duplicated Lines 17.77 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 1
dl 35
loc 197
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 48 1
A addClass() 0 4 1
A viewHeader() 0 19 1
A setText() 7 7 2
C getText() 28 28 7
A viewMenu() 0 15 3
A viewServices() 0 15 3
B view() 0 24 1
A getFileName() 0 8 2
A save() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace jumper423\decaptcha\core;
4
5
/**
6
 * Class DeCaptchaWikiMain.
7
 */
8
class DeCaptchaWikiMain extends DeCaptchaWiki
9
{
10
    protected $classes = [];
11
12
    public function __construct($class)
13
    {
14
        parent::__construct($class);
15
        $this->texts['slug_menu_desc'] = [
16
            'ru' => 'Описание',
17
            'en' => 'Description',
18
        ];
19
        $this->texts['readme_main_desc'] = [
20
            'ru' => 'Пакет создан для стандартизации всех сервисов по разгадыванию капч. 
21
            У каждого сервиса есть свои особенности и теперь Вам надо будет всего лишь взглянуть на документацию конкретного сервиса чтобы правильно всё сделать.
22
            Так же пакет покрывает всю функциональсть сервисов. Если же Вам будет чего-то нехватать или будут предложения, я буду только рад их услышать.',
23
            'en' => 'Package created to standardize all services for solving captcha.
24
            Each service has its own features and now You will have to look at the documentation for the specific service to do everything right. 
25
            The package covers the entire functionality services. If You will be something lacking or suggestions, I\'ll be glad to hear them.',
26
        ];
27
        $this->texts['slug_menu_services'] = [
28
            'ru' => 'Сервисы',
29
            'en' => 'Services',
30
        ];
31
        $this->texts['readme_main_services'] = [
32
            'ru' => 'Распознавание капч для всех популярных сервисов',
33
            'en' => 'Recognition captchas for all popular services',
34
        ];
35
        $this->texts['slug_menu_features'] = [
36
            'ru' => 'Особенности',
37
            'en' => 'Features',
38
        ];
39
        $this->texts['readme_main_features'] = [
40
            'ru' => [
41
                '+ Подходит для всех сервисов по распознаванию капч',
42
                '+ Можно легко добавить новый сервис, используя уже готовый движок',
43
                '+ Быстрая и интуительно понятная настройка',
44
                '+ Распознавание как по пути до файла, так и по ссылки',
45
                '+ ReCaptcha v2 без браузера',
46
                '+ Полная документация',
47
                '+ Покрыт тестами',
48
            ],
49
            'en' => [
50
                '+ Suitable for all recognition services captcha',
51
                '+ You can easily add a new service using the existing engine',
52
                '+ Intuitable fast and straightforward setup',
53
                '+ Recognition as the path to the file and links',
54
                '+ ReCaptcha v2 without a browser',
55
                '+ Full documentation',
56
                '+ Covered by tests',
57
            ],
58
        ];
59
    }
60
61
    public function addClass($class)
62
    {
63
        $this->classes[] = $class;
64
    }
65
66
    protected function viewHeader()
67
    {
68
        return 'DeCaptcha
69
================
70
[![Latest Stable Version](https://poser.pugx.org/jumper423/decaptcha/v/stable)](https://packagist.org/packages/jumper423/decaptcha)
71
[![Total Downloads](https://poser.pugx.org/jumper423/decaptcha/downloads)](https://packagist.org/packages/jumper423/decaptcha)
72
[![License](https://poser.pugx.org/jumper423/decaptcha/license)](https://packagist.org/packages/jumper423/decaptcha)
73
74
[![Build Status](https://travis-ci.org/jumper423/decaptcha.svg?branch=master)](https://travis-ci.org/jumper423/decaptcha)
75
[![Dependency Status](https://www.versioneye.com/user/projects/5849f365a662a5004c110a29/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/5849f365a662a5004c110a29)
76
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jumper423/decaptcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jumper423/decaptcha/?branch=master)
77
[![Build Status](https://scrutinizer-ci.com/g/jumper423/decaptcha/badges/build.png?b=master)](https://scrutinizer-ci.com/g/jumper423/decaptcha/build-status/master)
78
[![Code Climate](https://codeclimate.com/github/jumper423/decaptcha/badges/gpa.svg)](https://codeclimate.com/github/jumper423/decaptcha)
79
[![Issue Count](https://codeclimate.com/github/jumper423/decaptcha/badges/issue_count.svg)](https://codeclimate.com/github/jumper423/decaptcha)
80
[![codecov](https://codecov.io/gh/jumper423/decaptcha/branch/master/graph/badge.svg)](https://codecov.io/gh/jumper423/decaptcha)
81
[![HHVM Status](http://hhvm.h4cc.de/badge/jumper423/decaptcha.svg)](http://hhvm.h4cc.de/package/jumper423/decaptcha)
82
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/d485629c-1830-440d-82ab-a567bfa5ddc5/mini.png)](https://insight.sensiolabs.com/projects/d485629c-1830-440d-82ab-a567bfa5ddc5)
83
[![StyleCI](https://styleci.io/repos/75013766/shield?branch=master)](https://styleci.io/repos/75013766)'.PHP_EOL;
84
    }
85
86
    /**
87
     * @param string|array $name
88
     * @param string|array $value
89
     */
90 View Code Duplication
    public function setText($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        if (is_array($name)) {
93
            $name = implode('_', $name);
94
        }
95
        $this->texts[$name] = $value;
96
    }
97
98
    /**
99
     * @param string|array $name
100
     * @param string       $separator
101
     *
102
     * @return string|array
103
     */
104 View Code Duplication
    public function getText($name, $separator = '; ')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $getResult = function ($name, $texts) {
107
            if (is_array($name)) {
108
                $name = implode('_', $name);
109
            }
110
            if (!isset($texts[$name])) {
111
                return null;
112
            }
113
            if (is_array($texts[$name])) {
114
                if (isset($texts[$name][$this->lang])) {
115
                    return $texts[$name][$this->lang];
116
                }
117
118
                return array_values($texts[$name])[0];
119
            }
120
121
            return $texts[$name];
122
        };
123
        $result = $getResult($name, $this->texts);
124
        if (is_array($result)) {
125
            if ($separator) {
126
                $result = implode($separator, $result);
127
            }
128
        }
129
130
        return $result;
131
    }
132
133
    protected function viewMenu($master = '')
134
    {
135
        $str = "+ [{$this->getText(['slug', 'menu', 'another'])}](../{$master}docs/".$this->getFileName($this->lang == 'ru' ? 'en' : 'ru').')'.PHP_EOL;
136
        foreach ([
137
                     ['slug', 'menu', 'desc'],
138
                     ['slug', 'menu', 'features'],
139
                     ['slug', 'menu', 'services'],
140
                     ['install'],
141
                     ['example'],
142
                 ] as $anchor) {
143
            $str .= "+ [{$this->getText($anchor)}](#".implode('-', explode(' ', $this->getText($anchor))).')'.PHP_EOL;
144
        }
145
146
        return $str;
147
    }
148
149
    public function viewServices($master = '')
150
    {
151
        $str = '';
152
        $tt = [];
153
        foreach ($this->classes as $class) {
154
            if (isset($tt[$class->getWiki($this->lang)->getFileName()])) {
155
                continue;
156
            }
157
            $tt[$class->getWiki($this->lang)->getFileName()] = true;
158
            $fromServiceObjectWiki = $class->getWiki($this->lang);
159
            $str .= "+ [{$fromServiceObjectWiki->getText(['service', 'name'])}](../{$master}docs/{$fromServiceObjectWiki->getFileName()})".PHP_EOL;
160
        }
161
162
        return $str;
163
    }
164
165
    public function view($master = '')
166
    {
167
        $str = $this->viewHeader().PHP_EOL;
168
169
        $str .= "###{$this->getText(['slug', 'menu'])}".PHP_EOL;
170
        $str .= $this->viewMenu($master).PHP_EOL.PHP_EOL;
171
172
        $str .= "###{$this->getText(['slug', 'menu', 'desc'])}".PHP_EOL;
173
        $str .= $this->getText(['readme', 'main', 'desc']).PHP_EOL.PHP_EOL;
174
175
        $str .= "###{$this->getText(['slug', 'menu', 'features'])}".PHP_EOL;
176
        $str .= $this->getText(['readme', 'main', 'features'], PHP_EOL).PHP_EOL.PHP_EOL;
177
178
        $str .= "###{$this->getText(['slug', 'menu', 'services'])}".PHP_EOL;
179
        $str .= "{$this->getText(['readme', 'main', 'services'])}".PHP_EOL.PHP_EOL;
180
        $str .= "{$this->viewServices($master)}".PHP_EOL.PHP_EOL;
181
182
        $str .= "###{$this->getText(['install'])}".PHP_EOL;
183
        $str .= "{$this->viewInstall()}".PHP_EOL.PHP_EOL;
184
        $str .= "###{$this->getText(['example'])}".PHP_EOL;
185
        $str .= "{$this->viewExamples()}".PHP_EOL.PHP_EOL;
186
187
        return $str;
188
    }
189
190
    public function getFileName($lang = null)
191
    {
192
        if (is_null($lang)) {
193
            $lang = $this->lang;
194
        }
195
196
        return 'README-'.$lang.'.md';
197
    }
198
199
    public function save()
200
    {
201
        file_put_contents(__DIR__.'/../../docs/'.$this->getFileName(), $this->view());
202
        file_put_contents(__DIR__.'/../../README.md', $this->view('master/'));
203
    }
204
}
205