ApplicationUtils::asyncHelperCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebStream\Util;
3
4
use WebStream\Exception\SystemException;
5
6
/**
7
 * ApplicationUtils
8
 * アプリケーション依存のUtility
9
 * @author Ryuichi Tanaka
10
 * @since 2015/12/26
11
 * @version 0.7
12
 */
13
trait ApplicationUtils
14
{
15
    /**
16
     * プロジェクトディレクトリの絶対パスを返す
17
     * @return string プロジェクトディレクトリの絶対パス
18
     */
19
    public function getApplicationRoot()
20
    {
21
        // 上位階層を辿り、.projectrootファイルを見つける
22
        $targetPath = realpath(dirname(__FILE__));
23
        $isProjectRoot = false;
24
25
        while (!$isProjectRoot) {
26
            if (file_exists($targetPath . DIRECTORY_SEPARATOR . $this->getProjectFileName())) {
27
                $isProjectRoot = true;
28
            } else {
29
                if (preg_match("/(.*)\//", $targetPath, $matches)) {
30
                    $targetPath = $matches[1];
31
                    if (!is_dir($targetPath)) {
32
                        break;
33
                    }
34
                }
35
            }
36
        }
37
38
        if (!$isProjectRoot) {
0 ignored issues
show
introduced by
The condition $isProjectRoot is always true.
Loading history...
39
            throw new SystemException("'.projectroot' file must be put in directly under the project directory.");
40
        }
41
42
        return $targetPath;
43
    }
44
45
    /**
46
     * 指定したファイルの名前空間を取得する
47
     * @param string ファイルパス
0 ignored issues
show
Documentation Bug introduced by
The doc comment ファイルパス at position 0 could not be parsed: Unknown type name 'ファイルパス' at position 0 in ファイルパス.
Loading history...
48
     * @param string 起点ディレクトリパス
49
     * @return string 名前空間
50
     */
51
    public function getNamespace($filepath, $baseDir = null)
0 ignored issues
show
Unused Code introduced by
The parameter $baseDir is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
    public function getNamespace($filepath, /** @scrutinizer ignore-unused */ $baseDir = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        if (file_exists($filepath)) {
54
            $resource = fopen($filepath, "r");
55
            while (false !== ($line = fgets($resource))) {
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            while (false !== ($line = fgets(/** @scrutinizer ignore-type */ $resource))) {
Loading history...
56
                if (preg_match("/^namespace\s(.*);$/", $line, $matches)) {
57
                    $namespace = $matches[1];
58
                    if (substr($namespace, 0) !== '\\') {
59
                        $namespace = '\\' . $namespace;
60
                    }
61
62
                    return $namespace;
63
                }
64
            }
65
            fclose($resource);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            fclose(/** @scrutinizer ignore-type */ $resource);
Loading history...
66
        }
67
68
        return null;
69
    }
70
71
    /**
72
     * Viewで有効なModel変数名を返却する
73
     * @return string Model変数名
74
     */
75
    public function getModelVariableName()
76
    {
77
        return "model";
78
    }
79
80
    /**
81
     * Viewで有効なHelper変数名を返却する
82
     * @return string Helper変数名
83
     */
84
    public function getHelperVariableName()
85
    {
86
        return "helper";
87
    }
88
89
    /**
90
     * プロジェクトルートファイル名を返却する
91
     * @return string プロジェクトルートファイル名
92
     */
93
    private function getProjectFileName()
94
    {
95
        return ".projectroot";
96
    }
97
98
    /**
99
     * CoreHelper#asyncで使用するIDを返却する
100
     * @return string DOMID
101
     */
102
    public function getAsyncDomId()
103
    {
104
        return $this->getRandomstring(32);
0 ignored issues
show
Bug introduced by
It seems like getRandomstring() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
        return $this->/** @scrutinizer ignore-call */ getRandomstring(32);
Loading history...
105
    }
106
107
    /**
108
     * CoreHelper#asyncで使用するコードを返却する
109
     * @param string URL
110
     * @param string CSSクラス名
0 ignored issues
show
Documentation Bug introduced by
The doc comment CSSクラス名 at position 0 could not be parsed: Unknown type name 'CSSクラス名' at position 0 in CSSクラス名.
Loading history...
111
     * @return string コード
112
     */
113
    public function asyncHelperCode($url, $id)
114
    {
115
        return <<< JSCODE
116
(function (c,b) {var a;a=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP");a.onreadystatechange=function () {4==a.readyState&&200==a.status&&(document.getElementById(b).outerHTML=a.responseText)};a.open("GET",c,!0);a.send()})("$url","$id");
117
JSCODE;
118
    }
119
}
120