Skip to content

Commit

Permalink
optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyiw2013 committed Aug 19, 2021
1 parent 04c52cf commit 9bc1c86
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 58 deletions.
10 changes: 5 additions & 5 deletions src/block/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ export default class FunctionBlock extends Block
var type:string;

if (parts[2] != null && parts[1] === '?') {
type = TypeUtil.instance.getFormattedTypeByName(parts[2], true, head);
type = TypeUtil.instance.getResolvedTypeHints(parts[2], true, head);
} else if (parts[2] != null && parts[2] != "mixed" && parts[1] === undefined && parts[4] === "null") {// int $var = null
type = TypeUtil.instance.getFormattedTypeByName(parts[2], true, head);
type = TypeUtil.instance.getResolvedTypeHints(parts[2], true, head);
} else if (parts[2] != null) {
type = TypeUtil.instance.getFormattedTypeByName(parts[2], false, head);
type = TypeUtil.instance.getResolvedTypeHints(parts[2], false, head);
} else if (parts[4] != null && parts[4] != "") {
type = TypeUtil.instance.getFormattedTypeByName(TypeUtil.instance.getTypeFromValue(parts[4]), false,head);
type = TypeUtil.instance.getResolvedTypeHints(TypeUtil.instance.getTypeFromValue(parts[4]), false,head);
} else {
type = TypeUtil.instance.getUnknownType();
}
Expand All @@ -93,7 +93,7 @@ export default class FunctionBlock extends Block
head = this.getClassHead();
}
let nullable = returnType[1] === '?';
doc.return = TypeUtil.instance.getFormattedTypeByName(returnType[2], nullable, head);
doc.return = TypeUtil.instance.getResolvedTypeHints(returnType[2], nullable, head);
} else {
doc.return = this.getReturnFromName(params[5]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/block/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class Property extends Block

let nullable = parts[1] === '?';

doc.var = TypeUtil.instance.getFormattedTypeByName(parts[2], nullable, head);
doc.var = TypeUtil.instance.getResolvedTypeHints(parts[2], nullable, head);
} else if (params[6]) {
doc.var = TypeUtil.instance.getTypeFromValue(params[6]);
} else {
Expand Down
107 changes: 58 additions & 49 deletions src/util/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ export default class TypeUtil {
return this._instance || (this._instance = new this());
}

/**
* Resolve a type string that may contain union types
*
* @param {string} types
* @param {boolean} nullable
* @param {string} head
* @returns {string}
*/
public getResolvedTypeHints(types:string, nullable:boolean=false, head:string = null): string
{
let union:string[] = types.split("|");
let result:string[] = [];

for (let index = 0; index < union.length; index++) {
let type = union[index].trim();
if (type === '') {
continue;
}
if (head) {
type = this.getFullyQualifiedType(type, head);
}
type = this.getFormattedTypeByName(type);
result.push(type);
}

if (result.length === 0) {
result.push(this.getUnknownType());
} else if (nullable && result.indexOf('null') === -1) {
result.push('null');
}
return result.join('|');
}

/**
* Get the full qualified class namespace for a type
* we'll need to access the document
Expand Down Expand Up @@ -57,53 +90,29 @@ export default class TypeUtil {
* Returns the user configuration based name for the given type
*
* @param {string} name
* @param {boolean} nullable
* @param {string} head
*/
public getFormattedTypeByName(name:string, nullable:boolean=false, head:string=undefined) {
let result = [];
let names = name.split("|");
for (let index = 0; index < names.length; index++) {
names[index] = names[index].trim();
switch (names[index]) {
case '':
continue;
case 'real':
case 'double':
names[index] = 'float';
break;
case 'unset':
names[index] = 'null';
break;
case 'bool':
case 'boolean':
if (Config.instance.get('useShortNames')) {
names[index] = 'bool';
} else {
names[index] = 'boolean';
}
break;
case 'int':
case 'integer':
if (Config.instance.get('useShortNames')) {
names[index] = 'int';
} else {
names[index] = 'integer';
}
break;
default:
if (head) {
names[index] = TypeUtil.instance.getFullyQualifiedType(names[index], head);
}
}
result.push(names[index]);
}
if (result.length === 0) {
result.push(this.getUnknownType());
} else if (nullable && result.indexOf('null') === -1) {
result.push('null');
public getFormattedTypeByName(name:string) {
switch (name) {
case 'bool':
case 'boolean':
if (!Config.instance.get('useShortNames')) {
return 'boolean';
}
return 'bool';
case 'int':
case 'integer':
if (!Config.instance.get('useShortNames')) {
return 'integer';
}
return 'int';
case 'real':
case 'double':
return 'float';
case 'unset':
return 'null';
default:
return name;
}
return result.join('|');
}

/**
Expand Down Expand Up @@ -143,12 +152,12 @@ export default class TypeUtil {
{
// Check for bool `false|true` `!exp`
if (value.match(/^\s*(false|true)\s*$/i) !== null || value.match(/^\s*\!/i) !== null) {
return TypeUtil.instance.getFormattedTypeByName('bool');
return this.getFormattedTypeByName('bool');
}

// Check for int `-1` `1` `1_000_000`
if (value.match(/^\s*(\-?\d[\d_]*)\s*$/) !== null) {
return TypeUtil.instance.getFormattedTypeByName('int');
return this.getFormattedTypeByName('int');
}

// Check for float `.1` `1.1` `-1.1` `0.1_000_1`
Expand Down Expand Up @@ -184,9 +193,9 @@ export default class TypeUtil {
// Check for type casting
var match = value.match(/^\s*\(\s*(int|integer|bool|boolean|float|double|real|string|array|object|unset)\s*\)/i);
if (match) {
return TypeUtil.instance.getFormattedTypeByName(match[1]);
return this.getFormattedTypeByName(match[1]);
}

return TypeUtil.instance.getUnknownType();
return this.getUnknownType();
}
}
6 changes: 3 additions & 3 deletions test/TypeUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ suite("TypeUtil tests: ", () => {
test("Formatted type from namespace use with alias", () => {
let type = new TypeUtil;
Helper.setConfig({qualifyClassNames: true});
assert.equal(type.getFormattedTypeByName('BaseExample', true, head), '\\App\\Test\\Model\\Example|null');
assert.equal(type.getResolvedTypeHints('BaseExample', true, head), '\\App\\Test\\Model\\Example|null');
});

test("With default settings the integer type formatted is integer", () => {
Expand Down Expand Up @@ -88,12 +88,12 @@ suite("TypeUtil tests: ", () => {

test("Empty types", () => {
let type = new TypeUtil;
assert.equal(type.getFormattedTypeByName(' | | '), '[type]');
assert.equal(type.getResolvedTypeHints(' | | '), '[type]');
});

test("Default null", () => {
let type = new TypeUtil;
assert.equal(type.getFormattedTypeByName('string', true), 'string|null');
assert.equal(type.getResolvedTypeHints('string', true), 'string|null');
});

test("Default message - name", () => {
Expand Down

0 comments on commit 9bc1c86

Please sign in to comment.