Skip to content

Commit

Permalink
Merge pull request #256 from ILilliasI/pcn
Browse files Browse the repository at this point in the history
Add PCN metric
  • Loading branch information
yegor256 committed Mar 31, 2024
2 parents 18319c4 + d3cfce6 commit d697854
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions metrics/ast.py
Expand Up @@ -347,6 +347,17 @@ def varcomp(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> float:
return (parts / variables) if variables != 0 else 0


def pcn(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int:
"""Return number of words in the name of a class.
r:type: int
"""
classname = tlist[0][1].name
# By naming convention Java classes names use PascalCase.
# Nevertheless, this metric considers both PascalCasse and camelCase naming conventions.
words = re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', classname)
return len(words)


def nop(tlist: list[tuple[Any, javalang.tree.ClassDeclaration]]) -> int:
"""Return number of polymorphic methods in main class.
Methods of nested classes are skipped.
Expand Down Expand Up @@ -419,6 +430,8 @@ class NotClassError(Exception):
f'Number of Class Annotations\n')
metric.write(f'varcomp {varcomp(tree_class)} '
f'Average 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)} '
f'Method Hiding Factor (MHF), which is the ratio of private \
and protected methods to total methods\n')
Expand Down
1 change: 1 addition & 0 deletions tests/metrics/test-ast.sh
Expand Up @@ -49,6 +49,7 @@ stdout=$2
grep "final 1 " "${temp}/stdout"
grep "noca 1 " "${temp}/stdout"
grep "varcomp 2.75 " "${temp}/stdout"
grep "pcn 1" "${temp}/stdout"
grep "mhf 1.0 " "${temp}/stdout"
grep "smhf 0 " "${temp}/stdout"
grep "ahf 0.25 " "${temp}/stdout"
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/test-measure-file.sh
Expand Up @@ -45,7 +45,7 @@ EOT
set -x
test "$(echo "${msg}" | grep -c "sum=0")" = 0
all=$(find "${temp}" -name 'm1.*' -type f -exec basename {} \; | sort)
expected=49
expected=50
actual=$(echo "${all}" | wc -l | xargs)
if [ ! "${actual}" = "${expected}" ]; then
echo "Exactly ${expected} metrics were expected, but ${actual} were actually collected"
Expand Down

0 comments on commit d697854

Please sign in to comment.