Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PCN metric #256

Merged
merged 5 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions metrics/ast.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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