Skip to content

Commit

Permalink
PHP 7 types
Browse files Browse the repository at this point in the history
- PHP 7.0 return type declarations
- PHP 7.4 typed properties.
  • Loading branch information
ssigwart committed Oct 26, 2019
1 parent 1c3d95e commit 49826de
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/udl/skel/PHP/pylib/lang_php.py
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,7 @@ def decBlock(self):
self.currentFunction = None

def addFunction(self, name, phpArgs=None, attributes=None, doc=None,
returnType=None,
returnByRef=False):
log.debug("FUNC: %s(%r) on line %d", name, phpArgs, self.lineno)
classname = ''
Expand All @@ -2248,6 +2249,7 @@ def addFunction(self, name, phpArgs=None, attributes=None, doc=None,
doc=doc,
classname=classname,
classparent=extendsName,
returnType=returnType,
returnByRef=returnByRef)
if self.currentClass:
self.currentClass.functions[self.currentFunction.name] = self.currentFunction
Expand Down Expand Up @@ -2567,6 +2569,39 @@ def _getArgumentsFromPos(self, styles, text, pos):
phpArgs.append(phpArg)
return phpArgs, p

def _getFuncTypeHintFromPos(self, styles, text, pos):
"""Return function type hint"""

p = pos
citdl = None
if p < len(styles) and styles[p] == self.PHP_OPERATOR and text[p] == ":":
p += 1
citdl, p = self._getCidtlFromPos(styles, text, p)

return citdl, p

def _getCidtlFromPos(self, styles, text, pos):
"""Return type from position"""

p = pos
citdl = None
while p < len(styles):
if styles[p] in (self.PHP_IDENTIFIER, self.PHP_WORD):
if citdl is None:
citdl = ""
citdl += text[p]
# Namespace separator
elif text[p] == '\\':
if citdl is None:
citdl = ""
citdl += text[p]
elif styles[p] == self.PHP_OPERATOR and text[p] == "?" and citdl is None:
pass
else:
break
p += 1
return citdl, p

def _getOneIdentifierFromPos(self, styles, text, pos, identifierStyle=None):
if identifierStyle is None:
identifierStyle = self.PHP_IDENTIFIER
Expand Down Expand Up @@ -2918,6 +2953,7 @@ def _handleVariableComment(self, namelist, comment):
return False

def _variableHandler(self, styles, text, p, attributes, doc=None,
typeHint=None,
style="variable"):
log.debug("_variableHandler:: style: %r, text: %r, attributes: %r",
style, text[p:], attributes)
Expand Down Expand Up @@ -3009,6 +3045,8 @@ def _variableHandler(self, styles, text, p, attributes, doc=None,
if typeNames and p < len(styles) and \
styles[p] == self.PHP_OPERATOR and text[p] == "(":
p = self._skipPastParenArguments(styles, text, p+1)
if typeHint is not None:
typeNames.append(typeHint)

# Create the variable cix information.
if mustCreateVariable or (not thisVar and p < len(styles) and
Expand Down Expand Up @@ -3194,12 +3232,22 @@ def _addCodePiece(self, newstate=S_DEFAULT, varnames=None):
self.comment = None

# Eat special attribute keywords
likelyAttribute = False
while firstStyle == self.PHP_WORD and \
text[pos] in ("var", "public", "protected", "private",
"final", "static", "abstract"):
attributes.append(text[pos])
pos += 1
firstStyle = styles[pos]
likelyAttribute = True
attributeType = None
if likelyAttribute:
# Eat optional type hint
if firstStyle == self.PHP_OPERATOR and text[pos] == "?":
pos += 1
firstStyle = styles[pos]
attributeType, pos = self._getCidtlFromPos(styles, text, pos)
firstStyle = styles[pos]

if firstStyle == self.PHP_WORD:
keyword = text[pos].lower()
Expand Down Expand Up @@ -3250,8 +3298,10 @@ def _addCodePiece(self, newstate=S_DEFAULT, varnames=None):
"%r, line: %d in file: %r", namelist,
self.lineno, self.filename)
return
returnType, p = self._getFuncTypeHintFromPos(styles, text, p + 1) # +1 to skip ending parenthesis
self.addFunction(namelist[0], phpArgs, attributes,
doc=self.comment,
returnType=returnType,
returnByRef=returnByRef)
elif keyword == "class" or keyword == "trait":
# Examples:
Expand Down Expand Up @@ -3366,6 +3416,7 @@ def _addCodePiece(self, newstate=S_DEFAULT, varnames=None):
elif firstStyle == self.PHP_VARIABLE:
# Defining scope for action
self._variableHandler(styles, text, pos, attributes,
typeHint=attributeType,
doc=self.comment)
else:
log.debug("Unhandled first style:%d", firstStyle)
Expand Down

0 comments on commit 49826de

Please sign in to comment.