In order to really understand phenotypes, it helps to have a basic understanding of medical diagnosis and treatment ; of health systems and how data is recorded ; and of data quality. For several of your questions, there are problems of multiple uses of a medication for treatment or a lab for diagnosis. For instance, metformin is a medication used for diabetes and polycystic ovarian syndrome and prediabetes. For others, there is a problem of data completeness. The diagnosis of diabetes may be missing from the ehr but the other data may still indicate diabetes. PheKB used this kind of pragmatic approach to maximize recall and precision given both these issues.
Tl;dr medicine is complex, the data is dirty, and phenotype algorithms have to take these issues into account.