Skip to content

Commit

Permalink
Add inferring of type in properties
Browse files Browse the repository at this point in the history
  • Loading branch information
neild3r committed Mar 16, 2017
1 parent 582e754 commit 1d7c602
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to the "php-docblocker" extension will be documented in this file.

## [0.4.0] - 2017-03-17
- Add Inferring of types in properties

## [0.3.3] - 2017-03-16
- Fix matching of multiline class properties
- Fix the falling back to a simple block
Expand Down Expand Up @@ -40,4 +43,4 @@ All notable changes to the "php-docblocker" extension will be documented in this
[0.3.0]: https://github.com/neild3r/vscode-php-docblocker/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/neild3r/vscode-php-docblocker/compare/v0.1.1...v0.2.0
[0.2.0]: https://github.com/neild3r/vscode-php-docblocker/compare/v0.1.1...v0.2.0
[0.1.1]: https://github.com/neild3r/vscode-php-docblocker/compare/v0.1.0...v0.1.1
[0.1.1]: https://github.com/neild3r/vscode-php-docblocker/compare/v0.1.0...v0.1.1
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "php-docblocker",
"displayName": "PHP DocBlocker",
"description": "A simple, dependency free PHP specific DocBlocking package",
"version": "0.3.3",
"version": "0.4.0",
"publisher": "neilbrayfield",
"author": "Neil Brayfield <[email protected]>",
"engines": {
Expand Down Expand Up @@ -59,4 +59,4 @@
"@types/node": "^6.0.40",
"@types/mocha": "^2.2.32"
}
}
}
37 changes: 36 additions & 1 deletion src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,40 @@ export abstract class Block
return context.substr(0, endPos);
}

getTypeFromValue(value:string) {

let result:Array<string>;

// Check for cast
if (result = value.match(/^\s*(\([a-z]\))/)) {
return result[1];
}

// Check for bool
if (value.match(/^\s*(false|true)\s*$/i) !== null) {
return 'boolean';
}

// Check for int
if (value.match(/^\s*([\d-]+)\s*$/) !== null) {
return 'int';
}

// Check for float
if (value.match(/^\s*([\d.-])\s*$/) !== null) {
return 'float';
}

// Check for string
if (value.match(/^\s*(["'])/) !== null) {
return 'string';
}

// Check for array
if (value.match(/^\s*(array\(|\[)/) !== null) {
return 'array';
}
}

abstract parse():Doc;
}
}
9 changes: 7 additions & 2 deletions src/block/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import { Doc, Param } from "../doc";

export default class Property extends Block {

protected pattern:RegExp = /^\s*(static)?\s*(protected|private|public)\s+(static)?\s*(\$[A-Za-z0-9_]+)\s*\=?\s*(.*)$/m;
protected pattern:RegExp = /^\s*(static)?\s*(protected|private|public)\s+(static)?\s*(\$[A-Za-z0-9_]+)\s*\=?\s*([^;]*)/m;

parse():Doc {
let params = this.match();

let doc = new Doc('Undocumented variable');
doc.var = '[type]';

if (params[5]) {
doc.var = this.getTypeFromValue(params[5]);
} else {
doc.var = '[type]';
}

return doc;
}
Expand Down
36 changes: 24 additions & 12 deletions test/fixtures/properties.php.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
[
{
"key": "public",
"name": "Public"
"name": "Public",
"var": "[type]"
},
{
"key": "protected",
"name": "Protected"
"name": "Protected",
"var": "[type]"
},
{
"key": "static",
"name": "Static"
"name": "Static",
"var": "[type]"
},
{
"key": "static-alternate",
"name": "Static Alternate"
"name": "Static Alternate",
"var": "[type]"
},
{
"key": "default-string",
"name": "Default String"
"name": "Default String",
"var": "string"
},
{
"key": "default-string-alternate",
"name": "Default String Alternate"
"name": "Default String Alternate",
"var": "string"
},
{
"key": "default-bool",
"name": "Default Bool"
"name": "Default Bool",
"var": "boolean"
},
{
"key": "default-bool-alternate",
"name": "Default Bool Alternate"
"name": "Default Bool Alternate",
"var": "boolean"
},
{
"key": "default-array",
"name": "Default Array"
"name": "Default Array",
"var": "array"
},
{
"key": "default-array-alternate",
"name": "Default Array Alternate"
"name": "Default Array Alternate",
"var": "array"
},
{
"key": "multiline",
"name": "Multiline"
"name": "Multiline",
"var": "array"
},
{
"key": "multiline-alternate",
"name": "Multiline Alternate"
"name": "Multiline Alternate",
"var": "array"
}
]
6 changes: 6 additions & 0 deletions test/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ suite("Property tests", () => {
let func = new Function(testPositions[testData.key], editor);
assert.ok(func.parse(), test.name);
});

test("Type Test: "+ testData.name, () => {
let func = new Function(testPositions[testData.key], editor);
let doc:Doc = func.parse();
assert.equal(doc.var, testData.var, test.name);
});
});
});

0 comments on commit 1d7c602

Please sign in to comment.