Skip to content

Commit

Permalink
Add mx/mn pvn
Browse files Browse the repository at this point in the history
  • Loading branch information
ILilliasI committed Mar 31, 2024
1 parent baba12e commit 78fce49
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 deletions metrics/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,26 @@ def annts(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int:
"""
return len(tlist[0][1].annotations or [])

def _components_number(name: str) -> int:
"""Return number of parts in variable name.
Variables are split considering "$" and "_" symbols, snake_case and camelCase naming conventions.
r:type: int
"""
parts = 0
components = [comp for comp in re.split(r'[\$_]+', name) if comp != '']
for component in components:
parts += 1
prev_char = component[:1]
for char in component[1:]:
if prev_char.isdigit() != char.isdigit():
parts += 1
elif prev_char.islower() and char.isupper():
parts += 1
prev_char = char
if len(components) == 0:
parts += 1
return parts


def pvn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> float:
"""Return average number of parts in variable names in class.
Expand All @@ -329,24 +349,37 @@ def pvn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> float:
if (name := node.name) == '':
continue
variables += 1
# Java only allows "$" and "_" as special symbols in variable names.
# Split variable name by them to only count words and numbers.
components = [comp for comp in re.split(r'[\$_]+', name) if comp != '']
if len(components) == 0:
parts += 1
continue
for component in components:
parts += 1
prev_char = component[:1]
for char in component[1:]:
if prev_char.isdigit() != char.isdigit():
parts += 1
elif prev_char.islower() and char.isupper():
parts += 1
prev_char = char
parts += _components_number(name)
return (parts / variables) if variables != 0 else 0


def mxpvn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int:
"""Return maximum number of parts in variable names in class.
r:type: int
"""
max_parts = 0
for _, node in tlist[0][1].filter(javalang.tree.VariableDeclarator):
if (name := node.name) == '':
continue
name_parts = _components_number(name)
max_parts = max(name_parts, max_parts)
return max_parts


def mnpvn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int:
"""Return minimun number of parts in variable names in class.
r:type: int
"""
min_parts = 0
for _, node in tlist[0][1].filter(javalang.tree.VariableDeclarator):
if (name := node.name) == '':
continue
name_parts = _components_number(name)
if min_parts == 0 or name_parts < min_parts:
min_parts = name_parts
return min_parts


def pcn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int:
"""Return number of words in the name of a class.
r:type: int
Expand Down Expand Up @@ -430,6 +463,10 @@ class NotClassError(Exception):
f'Number of Class Annotations\n')
metric.write(f'pvn {pvn(tree_class)} '
f'Average number of parts in variable names\n')
metric.write(f'mxpvn {mxpvn(tree_class)} '
f'Maximum number of parts in variable names\n')
metric.write(f'mnpvn {mnpvn(tree_class)} '
f'Minimum number of parts in variable names\n')
metric.write(f'pcn {pcn(tree_class)} '
f'Number of words in the name of a class\n')
metric.write(f'mhf {mhf(tree_class)} '
Expand Down

0 comments on commit 78fce49

Please sign in to comment.