RisolutoCliBase   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 1 Features 2
Metric Value
wmc 13
c 2
b 1
f 2
lcom 0
cbo 1
dl 0
loc 133
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B readFromStdin() 0 30 3
A detectSeparator() 0 21 4
B separateOptions() 0 29 5
1
<?php
2
/**
3
 * RisolutoCliBase
4
 *
5
 * ユーザCliアプリ向けコントローラ用基底クラス
6
 *
7
 * @package           risoluto
8
 * @author            Risoluto Developers
9
 * @license           http://opensource.org/licenses/bsd-license.php new BSD license
10
 * @copyright     (C) 2008-2015 Risoluto Developers / All Rights Reserved.
11
 */
12
13
//------------------------------------------------------//
14
// 名前空間の定義
15
//------------------------------------------------------//
16
namespace Risoluto;
17
18
abstract class RisolutoCliBase
19
{
20
    //------------------------------------------------------//
21
    // クラスメソッド定義
22
    //------------------------------------------------------//
23
    use RisolutoErrorLogTrait;
24
25
    /**
26
     * __construct()
27
     *
28
     * コンストラクタ
29
     */
30
    public function __construct()
31
    {
32
    }
33
34
    /**
35
     * readFromStdin($prompt = 'Enter: ', $echoback = false)
36
     *
37
     * 標準入力からの入力を処理する
38
     *
39
     * @access    protected
40
     *
41
     * @param     string  $message ブラウザ側に出力するメッセージ(省略可、デフォルトは'Authorization Required')
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     * @param     boolean $echoback true:エコーバックする/false:エコーバックしない
43
     *
44
     * @return    string 入力された内容
45
     */
46
    protected function readFromStdin( $prompt = 'Enter: ', $echoback = true )
47
    {
48
        // 入出力のファイルハンドラをオープン
49
        $in = fopen( 'php://stdin', 'r' );
50
        $out = fopen( 'php://stderr', 'w' );
51
52
        // プロンプトを出力し、エコーバックオフが指示されていれば止める
53
        fwrite( $out, $prompt );
54
        if (!$echoback) {
55
            system( 'stty -echo' );
56
        }
57
58
        // 入力をロックし、キー入力を取得する
59
        flock( $in, LOCK_EX );
60
        $readtext = fgets( $in );
61
        flock( $in, LOCK_UN );
62
63
        // エコーバックオフが指示されていれば再開し、PHP_EOLを出力
64
        if (!$echoback) {
65
            system( 'stty echo' );
66
        }
67
        fwrite( $out, PHP_EOL );
68
69
        // 入出力のファイルハンドラをクローズ
70
        fclose( $in );
71
        fclose( $out );
72
73
        // 取得内容を返却する
74
        return trim( $readtext );
75
    }
76
77
    /**
78
     * detectSeparator($target)
79
     *
80
     * 分割文字を検出する
81
     *
82
     * @access    private
83
     *
84
     * @param     string $target 対象となる文字列
85
     *
86
     * @return    string 検出した分割文字
87
     */
88
    private function detectSeparator( $target )
89
    {
90
        // 分割文字列として許容する文字列
91
        $allow_sepchars = [ '=', ',', ':' ];
92
93
        // 分割文字を検出する
94
        $retval = '';
95
        $before_position = PHP_INT_MAX;
96
        foreach ($allow_sepchars as $test) {
97
            $current_position = strpos( $target, $test );
98
99
            // 1つ前に検出した分割文字よりも先に存在する場合はそちらを採用
100
            if ($current_position !== false and $current_position < $before_position) {
101
                $retval = $test;
102
                $before_position = $current_position;
103
            }
104
        }
105
106
        // 検出した分割文字を返却する
107
        return $retval;
108
    }
109
110
    /**
111
     * separateOptions($options)
112
     *
113
     * オプションを分割する
114
     *
115
     * @access    protected
116
     *
117
     * @param     string $target 対象となる文字列
118
     *
119
     * @return    array 分割後のオプション
120
     */
121
    protected function separateOptions( $target )
122
    {
123
        // 分割文字を取得する
124
        $sepchar = $this->detectSeparator( $target );
125
126
        // 分割文字列が存在しているかをチェック
127
        if ($sepchar) {
128
            // 分割文字が存在した場合はそれで分割する
129
            $sep = explode( $sepchar, $target );
130
131
            if (count( $sep ) > 2) {
132
133
                // 2つより多く分割された場合は最初の1つと残りの全部に再構築する
134
                $command = array_shift( $sep );
135
                $param = $this->separateOptions( implode( $sepchar, $sep ) );
136
            } else {
137
                // 2つ以下ならそのまま($param側に分割文字がさらに含まれている場合は再帰的に分割する)
138
                $command = $sep[ 0 ];
139
                $param = ( isset( $sep[ 1 ] ) ? ( $this->detectSeparator( $sep[ 1 ] ) ? $this->separateOptions( $sep[ 1 ] ) : $sep[ 1 ] ) : '' );
140
            }
141
        } else {
142
            //存在しない場合はそのまま
143
            $command = $target;
144
            $param = '';
145
        }
146
147
        // 分割したものを返却する
148
        return [ 'command' => $command, 'param' => $param ];
149
    }
150
}
151