Browse Source

Doc parser

tags/0.1.11
Robin Thoni 8 years ago
parent
commit
5e75e756dc

+ 159
- 0
src/Utils/Business/LuDocParser.php View File

@@ -0,0 +1,159 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 5/29/16
6
+ * Time: 7:42 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Business;
10
+
11
+use Luticate\Utils\Dbo\LuParameterConstraintDbo;
12
+use Luticate\Utils\Dbo\LuParameterDbo;
13
+
14
+class LuDocParser
15
+{
16
+    /**
17
+     * @var string
18
+     */
19
+    private $_doc;
20
+
21
+    /**
22
+     * @return string
23
+     */
24
+    public function getSummary()
25
+    {
26
+        return $this->_summary;
27
+    }
28
+
29
+    /**
30
+     * @var string
31
+     */
32
+    private $_summary = "";
33
+
34
+    /**
35
+     * @var LuParameterDbo[]
36
+     */
37
+    private $_params = [];
38
+
39
+    /**
40
+     * @var string
41
+     */
42
+    private $_returnType = "";
43
+
44
+    /**
45
+     * @return string
46
+     */
47
+    public function getDoc()
48
+    {
49
+        return $this->_doc;
50
+    }
51
+
52
+    /**
53
+     * @return \Luticate\Utils\Dbo\LuParameterDbo[]
54
+     */
55
+    public function getParams()
56
+    {
57
+        return $this->_params;
58
+    }
59
+
60
+    /**
61
+     * @return string
62
+     */
63
+    public function getReturnType()
64
+    {
65
+        return $this->_returnType;
66
+    }
67
+
68
+    /**
69
+     * LuDocParser constructor.
70
+     * @param $doc string
71
+     */
72
+    public function __construct($doc)
73
+    {
74
+        $this->_doc = $doc;
75
+    }
76
+    
77
+    public function parse()
78
+    {
79
+        if (!is_string($this->_doc)) {
80
+            return;
81
+        }
82
+        $lines = preg_split("/(\r?\n)/", $this->_doc);
83
+        $count = count($lines);
84
+        if ($count > 2) {
85
+            array_splice($lines, 0, 1);
86
+            array_splice($lines, $count - 2, 1);
87
+        }
88
+
89
+        $currentParam = null;
90
+        
91
+        foreach ($lines as $line) {
92
+            $lineMatches = [];
93
+            if (preg_match("/ *\\** *(.*) */", $line, $lineMatches) === 1) {
94
+                $line = $lineMatches[1];
95
+                $commandMatches = [];
96
+                if (preg_match("/@([^ ]+) *(.*)/", $line, $commandMatches)) {
97
+                    $command = strtolower($commandMatches[1]);
98
+                    $line = $commandMatches[2];
99
+                    if ($command == "param") {
100
+                        $currentParam = new LuParameterDbo();
101
+                        $paramMatches = [];
102
+                        if (preg_match("/([^ ]+) *([^ ]+) *(.*)/", $line, $paramMatches) === 1) {
103
+                            if ($paramMatches[1][0] == "$") {
104
+                                $currentParam->setName($paramMatches[1]);
105
+                                $currentParam->setType($paramMatches[2]);
106
+                            }
107
+                            else {
108
+                                $currentParam->setName($paramMatches[2]);
109
+                                $currentParam->setType($paramMatches[1]);
110
+                            }
111
+                            $currentParam->setSummary($paramMatches[3] . "\n");
112
+                        }
113
+                        $this->_params[$currentParam->getName()] = $currentParam;
114
+                    }
115
+                    else if ($command == "deprecated") {
116
+                    }
117
+                    else if ($command == "inheritdoc") {
118
+                    }
119
+                    else if ($command == "internal") {
120
+                    }
121
+                    else if ($command == "link") {
122
+                    }
123
+                    else if ($command == "return") {
124
+                        $this->_returnType = $line;
125
+                    }
126
+                    else if ($command == "see") {
127
+                    }
128
+                    else if ($command == "throws") {
129
+                    }
130
+                    else if (!is_null($currentParam)) {
131
+                        $methodName = $command;
132
+                        $constraint = new LuParameterConstraintDbo();
133
+                        $constraint->setMethod($methodName);
134
+                        $args = [];
135
+                        $argMatches = [];
136
+                        if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|"[^"]*"))/', $line, $argMatches) !== false) {
137
+                            $args = $argMatches[1];
138
+                            foreach ($args as $key => $arg) {
139
+                                if ($arg[0] == '"') {
140
+                                    $args[$key] = substr($arg, 1, count($arg) - 2);
141
+                                }
142
+                            }
143
+                        }
144
+                        $constraint->setArguments($args);
145
+                        $currentParam->addConstraint($constraint);
146
+                    }
147
+                }
148
+                else {
149
+                    if (is_null($currentParam)) {
150
+                        $this->_summary .= $line . "\n";
151
+                    }
152
+                    else {
153
+                        $currentParam->setSummary($currentParam->getSummary() . $line . "\n");
154
+                    }
155
+                }
156
+            }
157
+        }
158
+    }
159
+}

+ 15
- 0
src/Utils/Dbo/LuDboConstraintException.php View File

@@ -0,0 +1,15 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 5/29/16
6
+ * Time: 7:18 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+
12
+class LuDboConstraintException extends \Exception
13
+{
14
+
15
+}

+ 21
- 0
src/Utils/Dbo/LuFloatDbo.php View File

@@ -54,4 +54,25 @@ class LuFloatDbo extends LuDbo
54 54
     {
55 55
         return 42.42;
56 56
     }
57
+
58
+    public function between($min, $max)
59
+    {
60
+        if ($this->_value < $min || $this->_value > $max) {
61
+            throw new LuDboConstraintException("Float must be between ${min} and ${max} inclusive");
62
+        }
63
+    }
64
+
65
+    public function min($min)
66
+    {
67
+        if ($this->_value < $min) {
68
+            throw new LuDboConstraintException("Float must be greater or equal than ${min}");
69
+        }
70
+    }
71
+
72
+    public function max($max)
73
+    {
74
+        if ($this->_value > $max) {
75
+            throw new LuDboConstraintException("Float must be smaller or equal than ${max}");
76
+        }
77
+    }
57 78
 }

+ 22
- 0
src/Utils/Dbo/LuIntDbo.php View File

@@ -54,4 +54,26 @@ class LuIntDbo extends LuDbo
54 54
     {
55 55
         return 42;
56 56
     }
57
+
58
+    public function between($min, $max)
59
+    {
60
+        if ($this->_value < $min || $this->_value > $max) {
61
+            throw new LuDboConstraintException("Integer must be between ${min} and ${max} inclusive");
62
+        }
63
+    }
64
+    
65
+    public function min($min)
66
+    {
67
+        if ($this->_value < $min) {
68
+            throw new LuDboConstraintException("Integer must be greater or equal than ${min}");
69
+        }
70
+    }
71
+
72
+    public function max($max)
73
+    {
74
+        if ($this->_value > $max) {
75
+            throw new LuDboConstraintException("Integer must be smaller or equal than ${max}");
76
+        }
77
+    }
78
+    
57 79
 }

+ 65
- 0
src/Utils/Dbo/LuParameterConstraintDbo.php View File

@@ -0,0 +1,65 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 5/29/16
6
+ * Time: 8:52 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+
12
+use Luticate\Utils\LuDbo;
13
+
14
+class LuParameterConstraintDbo extends LuDbo
15
+{
16
+    /**
17
+     * @var $_method string
18
+     */
19
+    private $_method;
20
+
21
+    /**
22
+     * @var $_arguments string[]
23
+     */
24
+    private $_arguments = [];
25
+
26
+    function jsonSerialize()
27
+    {
28
+        return [
29
+            "Method" => $this->_method,
30
+            "Arguments" => $this->_arguments
31
+        ];
32
+    }
33
+
34
+    /**
35
+     * @return string
36
+     */
37
+    public function getMethod()
38
+    {
39
+        return $this->_method;
40
+    }
41
+
42
+    /**
43
+     * @param string $method
44
+     */
45
+    public function setMethod($method)
46
+    {
47
+        $this->_method = $method;
48
+    }
49
+
50
+    /**
51
+     * @return \string[]
52
+     */
53
+    public function getArguments()
54
+    {
55
+        return $this->_arguments;
56
+    }
57
+
58
+    /**
59
+     * @param \string[] $arguments
60
+     */
61
+    public function setArguments($arguments)
62
+    {
63
+        $this->_arguments = $arguments;
64
+    }
65
+}

+ 117
- 0
src/Utils/Dbo/LuParameterDbo.php View File

@@ -0,0 +1,117 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 5/29/16
6
+ * Time: 7:48 PM
7
+ */
8
+
9
+namespace Luticate\Utils\Dbo;
10
+
11
+use Luticate\Utils\LuDbo;
12
+
13
+class LuParameterDbo extends LuDbo
14
+{
15
+    /**
16
+     * @var $_summary string
17
+     */
18
+    private $_summary = "";
19
+
20
+    /**
21
+     * @return string
22
+     */
23
+    public function getSummary()
24
+    {
25
+        return $this->_summary;
26
+    }
27
+
28
+    /**
29
+     * @param string $summary
30
+     */
31
+    public function setSummary($summary)
32
+    {
33
+        $this->_summary = $summary;
34
+    }
35
+
36
+    /**
37
+     * @var $_type string
38
+     */
39
+    private $_type = "";
40
+
41
+    /**
42
+     * @var $_name string
43
+     */
44
+    private $_name = "";
45
+
46
+    /**
47
+     * @return string
48
+     */
49
+    public function getType()
50
+    {
51
+        return $this->_type;
52
+    }
53
+
54
+    /**
55
+     * @param string $type
56
+     */
57
+    public function setType($type)
58
+    {
59
+        $this->_type = $type;
60
+    }
61
+
62
+    /**
63
+     * @return string
64
+     */
65
+    public function getName()
66
+    {
67
+        return $this->_name;
68
+    }
69
+
70
+    /**
71
+     * @param string $name
72
+     */
73
+    public function setName($name)
74
+    {
75
+        $this->_name = $name;
76
+    }
77
+
78
+    /**
79
+     * @return LuParameterConstraintDbo[]
80
+     */
81
+    public function getConstraints()
82
+    {
83
+        return $this->_constraints;
84
+    }
85
+
86
+    /**
87
+     * @param LuParameterConstraintDbo[] $constraints
88
+     */
89
+    public function setConstraints($constraints)
90
+    {
91
+        $this->_constraints = $constraints;
92
+    }
93
+    
94
+    public function addConstraint($constraint)
95
+    {
96
+        $this->_constraints[] = $constraint;
97
+    }
98
+
99
+    /**
100
+     * @var $_constraints LuParameterConstraintDbo[]
101
+     */
102
+    private $_constraints = [];
103
+    
104
+    function jsonSerialize()
105
+    {
106
+        $constraints = [];
107
+        foreach ($this->_constraints as $constraint) {
108
+            $constraints[] = $constraint->jsonSerialize();
109
+        }
110
+        return [
111
+            "Name" => $this->_name,
112
+            "Type" => $this->_type,
113
+            "Summary" => $this->_summary,
114
+            "Constraints" => $constraints
115
+        ];
116
+    }
117
+}

+ 122
- 0
tests/LuDocParserTest.php View File

@@ -0,0 +1,122 @@
1
+<?php
2
+use Luticate\Utils\Business\LuDocParser;
3
+
4
+/**
5
+ * Created by PhpStorm.
6
+ * User: robin
7
+ * Date: 5/29/16
8
+ * Time: 7:52 PM
9
+ */
10
+class LuDocParserTest extends \PHPUnit_Framework_TestCase
11
+{
12
+    public function testSimpleSummary()
13
+    {
14
+        $doc = new LuDocParser("/**
15
+     * Get all data
16
+     */");
17
+        $doc->parse();
18
+        $this->assertSame("Get all data\n", $doc->getSummary());
19
+    }
20
+    
21
+    public function testSimpleSummaryMultiLine()
22
+    {
23
+        $doc = new LuDocParser("/**
24
+     * Get all data
25
+     * Using the database
26
+     */");
27
+        $doc->parse();
28
+        $this->assertSame("Get all data\nUsing the database\n", $doc->getSummary());
29
+    }
30
+
31
+    public function testSimpleParam()
32
+    {
33
+        $doc = new LuDocParser('/**
34
+     * Get all data
35
+     * @param $myvar MyType some doc
36
+     * @param MyType $myvar2 some other doc
37
+     */');
38
+        $doc->parse();
39
+        $this->assertSame("Get all data\n", $doc->getSummary());
40
+        
41
+        $params = [];
42
+        foreach ($doc->getParams() as $param) {
43
+            $params[] = $param->jsonSerialize();
44
+        }
45
+        
46
+        $expectedParams = [
47
+            ["Name" => '$myvar', "Type" => "MyType", "Summary" => "some doc\n",
48
+                "Constraints" => []],
49
+            ["Name" => '$myvar2', "Type" => "MyType", "Summary" => "some other doc\n",
50
+                "Constraints" => []]
51
+        ];
52
+        
53
+        $this->assertSame($expectedParams, $params);
54
+    }
55
+
56
+    public function testSimpleParamMultiLine()
57
+    {
58
+        $doc = new LuDocParser('/**
59
+     * Get all data
60
+     * @param $myvar MyType some doc
61
+     * @param MyType $myvar2 some other doc
62
+     * on another line
63
+     */');
64
+        $doc->parse();
65
+        $this->assertSame("Get all data\n", $doc->getSummary());
66
+
67
+        $params = [];
68
+        foreach ($doc->getParams() as $name => $param) {
69
+            $params[$name] = $param->jsonSerialize();
70
+        }
71
+
72
+        $expectedParams = [
73
+            '$myvar' => ["Name" => '$myvar', "Type" => "MyType", "Summary" => "some doc\n",
74
+                "Constraints" => []],
75
+            '$myvar2' => ["Name" => '$myvar2', "Type" => "MyType", "Summary" => "some other doc\non another line\n",
76
+                "Constraints" => []]
77
+        ];
78
+
79
+        $this->assertSame($expectedParams, $params);
80
+    }
81
+
82
+    public function testAll()
83
+    {
84
+        $doc = new LuDocParser('/**
85
+     * Get all data
86
+     * On another line
87
+     * @param $myvar MyType some doc
88
+     * Too much doc
89
+     * @param MyType $myvar2 some other doc
90
+     * on another line
91
+     * @min 42
92
+     * @max 42
93
+     * @between 0 42
94
+     * @another 1 2 42.42 true false "a string"
95
+     * and another
96
+     * @return AnotherType
97
+     */');
98
+        $doc->parse();
99
+        $this->assertSame("Get all data\nOn another line\n", $doc->getSummary());
100
+        $this->assertSame("AnotherType", $doc->getReturnType());
101
+
102
+        $params = [];
103
+        foreach ($doc->getParams() as $name => $param) {
104
+            $params[$name] = $param->jsonSerialize();
105
+        }
106
+
107
+        $expectedParams = [
108
+            '$myvar' => ["Name" => '$myvar', "Type" => "MyType", "Summary" => "some doc\nToo much doc\n",
109
+                "Constraints" => []],
110
+            '$myvar2' => ["Name" => '$myvar2', "Type" => "MyType", "Summary" => "some other doc\non another line\nand another\n",
111
+                "Constraints" => [
112
+                    ["Method" => "min", "Arguments" => ["42"]],
113
+                    ["Method" => "max", "Arguments" => ["42"]],
114
+                    ["Method" => "between", "Arguments" => ["0", "42"]],
115
+                    ["Method" => "another", "Arguments" => ["1", "2", "42.42", "true", "false", "a string"]]
116
+                ]
117
+            ]
118
+        ];
119
+
120
+        $this->assertSame($expectedParams, $params);
121
+    }
122
+}

Loading…
Cancel
Save