Skip to content

Commit

Permalink
Merge 0f6a770 into 8e4162e
Browse files Browse the repository at this point in the history
  • Loading branch information
neild3r committed Mar 24, 2018
2 parents 8e4162e + 0f6a770 commit 6585575
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This extension contributes the following settings:
* `php-docblocker.returnGap`: set to `true` to add a gap between the param and return tags
* `php-docblocker.extra`: an array of extra tags to add to each DocBlock (These can include tabstops and snippet variables)
* `php-docblocker.useShortNames`: Whether we should use short type names. e.g. bool or boolean
* `php-docblocker.qualifyClassNames`: When adding type hints for class names search namespace use statements and qualify the class
* `php-docblocker.author`: An object containing your default author tag settings

## Supported DocBlock tags
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"default": false,
"description": "Wether you want to use integer instead of int and boolean instead of bool."
},
"php-docblocker.qualifyClassNames": {
"type": "boolean",
"default": false,
"description": "Fully qualifies any data types used in param and returns by reading the namespaces."
},
"php-docblocker.author": {
"type": "object",
"default": {
Expand Down
5 changes: 5 additions & 0 deletions src/block/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Block } from "../block";
import { Doc, Param } from "../doc";
import TypeUtil from "../util/TypeUtil";
import { Range, Position } from "vscode";

/**
* Represents a function code block
Expand Down Expand Up @@ -34,6 +35,10 @@ export default class FunctionBlock extends Block
let parts = arg.match(/^\s*(\?)?\s*([A-Za-z0-9_\\]+)?\s*\&?((?:[.]{3})?\$[A-Za-z0-9_]+)\s*\=?\s*(.*)\s*/m);
var type = '[type]';

if (parts[2] != null) {
parts[2] = TypeUtil.instance.getFullyQualifiedType(parts[2], this.editor.document);
}

if (parts[2] != null && parts[1] === '?') {
type = TypeUtil.instance.getFormattedTypeByName(parts[2])+'|null';
} else if (parts[2] != null) {
Expand Down
68 changes: 66 additions & 2 deletions src/util/TypeUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { workspace } from "vscode";
import { workspace, TextEditor, Range, Position, TextDocument } from "vscode";

/**
* Provides helper function to types
Expand All @@ -12,12 +12,20 @@ export default class TypeUtil {
private static _instance: TypeUtil;

/**
* Holds wether we use short names or not
* Holds whether we use short names or not
*
* @type {bool|null}
*/
private _useShortNames: any;


/**
* Whether we should qualify class names or not
*
* @type {bool|null}
*/
private _qualifyClassNames: any;

/**
* Returns the instance for this util
*
Expand Down Expand Up @@ -48,6 +56,62 @@ export default class TypeUtil {
return this._useShortNames;
}

/**
* Overwrites the value
*
* @param {boolean} value
*/
public set qualifyClassNames(value:boolean) {
this._qualifyClassNames = value;
}

/**
* Should we qualify class names
*/
public get qualifyClassNames() {
if (this._qualifyClassNames == null) {
let config: any = workspace.getConfiguration().get('php-docblocker');
this._qualifyClassNames = config.qualifyClassNames || false;
}

return this._qualifyClassNames;
}

/**
* Get the full qualified class namespace for a type
* we'll need to access the document
*
* @param {string} type
* @param {TextDocument} document
* @returns {string}
*/
public getFullyQualifiedType(type:string, document:TextDocument):string
{
if (!this.qualifyClassNames) {
return type;
}

let text = document.getText();
let regex = /\s*(abstract|final)?\s*(class|trait|interface)/gm;
let match = regex.exec(text);
let end = document.positionAt(match.index);
let range = new Range(new Position(0, 0), end);
let head = document.getText(range);

let useEx = new RegExp("use\\s+(.*?)((?:\\s+as\\s+))?"+type+";", 'gm');
let full = useEx.exec(head);

if (full != null) {
if (full[2] != null) {
return full[1];
}

return full[1] + type;
}

return type;
}

/**
* Returns the user configuration based name for the given type
*
Expand Down
27 changes: 26 additions & 1 deletion test/TypeUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import * as assert from 'assert';
import TypeUtil from "../src/util/TypeUtil";
import { window, workspace, TextDocument } from 'vscode';
import Helper from './helpers';

suite("TypeUtil tests: ", () => {
let textDocument:TextDocument;

suiteSetup(function(done) {
workspace.openTextDocument(Helper.fixturePath+'namespace.php').then(doc => {
textDocument = doc;
done();
}, error => {
console.log(error);
});
});


test("Fully qualify typehint from namespace", () => {
let type = new TypeUtil;
type.qualifyClassNames = true;
assert.equal(type.getFullyQualifiedType('FilterInterface', textDocument), 'App\\Test\\Model\\FilterInterface');
});

test("Fully qualify typehint from namespace use with alias", () => {
let type = new TypeUtil;
type.qualifyClassNames = true;
assert.equal(type.getFullyQualifiedType('BaseExample', textDocument), 'App\\Test\\Model\\Example');
});

test("With default settings the integer type formatted is integer", () => {
let type = new TypeUtil;
Expand Down Expand Up @@ -30,4 +55,4 @@ suite("TypeUtil tests: ", () => {
assert.equal(type.getFormattedTypeByName('helloWorld'), 'helloWorld');
});

});
});
21 changes: 21 additions & 0 deletions test/fixtures/namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Test;

use App\Test\Model\ModelInterface;
use App\Test\Model\ModelTrait;
use App\Test\Model\FilterInterface;
use App\Test\Model\Example as BaseExample;

use Monolog\Handler\HandlerInterface;
use Monolog\Handler\StreamHandler;
use Psr\Log\LoggerInterface;
use Psr\Log\InvalidArgumentException;

/**
* Example class here
*/
class Example
{

}

0 comments on commit 6585575

Please sign in to comment.