<commit_before>package io.gitlab.arturbosch.detekt.rules

import io.gitlab.arturbosch.detekt.api.CodeSmellRule
import io.gitlab.arturbosch.detekt.api.Config
import org.jetbrains.kotlin.psi.KtClassBody
import org.jetbrains.kotlin.psi.KtNamedFunction

/**
 * @author <NAME>
 */
class FeatureEnvy(config: Config = Config.empty) : CodeSmellRule("FeatureEnvy", config) {

	override fun visitClassBody(classBody: KtClassBody) {
		val properties = classBody.properties
		val functions = classBody.declarations
				.filterIsInstance(KtNamedFunction::class.java)



		super.visitClassBody(classBody)
	}

}<commit_msg>Implement function to collect all function calls - #36<commit_after>package io.gitlab.arturbosch.detekt.rules

import io.gitlab.arturbosch.detekt.api.CodeSmellRule
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.DetektVisitor
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.KtProperty

/**
 * @author <NAME>
 */
class FeatureEnvy(config: Config = Config.empty) : CodeSmellRule("FeatureEnvy", config) {

	override fun visitClass(klass: KtClass) {
		val properties = klass.getProperties()
		val functions = klass.declarations
				.filterIsInstance(KtNamedFunction::class.java)
		val visitor = FeatureEnvyClassVisitor(klass.nameAsSafeName.identifier, properties, functions)
		visitor.run()
		super.visitClass(klass)
	}

	class FeatureEnvyClassVisitor(val className: String,
								  val properties: List<KtProperty>,
								  val functions: List<KtNamedFunction>) : DetektVisitor() {

		fun run() {
			functions.filter { it.funKeyword != null }
					.filter { it.hasBlockBody() }
					.forEach { function -> analyzeFunction(function) }
		}

		private fun analyzeFunction(function: KtNamedFunction) {
			val allCalls = function.collectByType<KtCallExpression>()
			println("${function.name} " + allCalls.map { 1 }.sum())
		}
	}
}

inline fun <reified T : KtElement> KtNamedFunction.collectByType(): List<T> {
	val list = mutableListOf<T>()
	this.accept(object : DetektVisitor() {
		override fun visitKtElement(element: KtElement) {
			if (element is T) {
				list.add(element)
			}
			element.children.forEach { it.accept(this) }
		}
	})
	return list
}
{'fraction_non_alphanumeric': 0.11407972858354538, 'fraction_numerical': 0.001272264631043257, 'mean_word_length': 4.810344827586207, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7062190', 'n_tokens_mistral': 857, 'n_tokens_neox': 790, 'n_words': 138}